Python 如何使用factorplot注释带有分类值的条形图或绘制4个变量?

Python 如何使用factorplot注释带有分类值的条形图或绘制4个变量?,python,python-3.x,pandas,data-visualization,seaborn,Python,Python 3.x,Pandas,Data Visualization,Seaborn,我有一个要绘制的数据帧。我想到了两个选择(查看图片) 对于选项1,我需要注释一个分类值(“Elec”) 对于选项2,我仍然需要使用“factorplot”,但我不知道如何修复我得到的错误 #CODE FOR THE DATAFRAME raw_data = {'Max_Acc': [90.71, 87.98, 92.62, 78.93, 73.69, 73.66, 72.29, 92.62, 94.17, 92.62, 83.81, 79.76,

我有一个要绘制的数据帧。我想到了两个选择(查看图片)

对于选项1,我需要注释一个分类值(“Elec”)

对于选项2,我仍然需要使用“factorplot”,但我不知道如何修复我得到的错误

#CODE FOR THE DATAFRAME
raw_data = {'Max_Acc': [90.71, 87.98, 92.62, 78.93, 73.69, 73.66, 72.29,
                        92.62, 94.17, 92.62, 83.81, 79.76, 74.40, 72.38],
            'Stage': ['AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL',
                      'AWA', 'Rem', 'S1', 'S2', 'SWS', 'SX', 'ALL'],
             'Elec': ['Fp1', 'Fp2', 'C4', 'Cz', 'Pz', 'P4', 'T3',
                      'C4', 'T3', 'Fp1', 'P4', 'Fp2', 'Fz', 'Fz'],
            'Clf': ['RF', 'RF', 'RF', 'RF', 'RF', 'RF', 'RF',
                    'XG', 'XG', 'XG', 'XG', 'XG', 'XG', 'XG']}

df_m=pd.DataFrame(raw_data, columns = ['Max_Acc', 'Stage', 'Elec', 'Clf'])

df_m


#CODE FOR THE PLOT (OPTION 1)
sns.set(style="white") 
g = sns.factorplot(x="Stage", y="Elec", hue='Clf', data=df, size=2, aspect=3, kind="bar",
               legend=False) 


ax=g.ax 
def annotateBars(row, ax=ax): 
    for p in ax.patches:
        ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
         ha='center', va='center', fontsize=11, color='gray', rotation=90, xytext=(0, 20),
         textcoords='offset points')  


plot = df_m.apply(annotateBars, ax=ax, axis=1)



#CODE FOR THE PLOT (OPTION 2)
g = sns.factorplot(x="Clf", y="Max_Acc", hue='Elec', col='Stage', data=df, size=2, aspect=3, kind="bar",
               legend=False)
选项1(使用分类值进行注释)

选项2(绘制4个变量)

我没有使用“FactoryPlot”。 我刚刚插入了第二个x轴

我没有使用“factorplot”。 我刚刚插入了第二个x轴


你的错误是什么?我刚试过你的代码,你的选项2对我来说很好。你在图片下面看到的图就是错误。条形图是分开的,我无法对它们进行注释。您得到的错误是什么?我刚试过你的代码,你的选项2对我来说很好。你在图片下面看到的图就是错误。条形图是分开的,我无法对它们进行注释。
#To use seaborn palette
palette = sns.color_palette("Set1", 8)
sns.set(style="white")

uelec, uind = np.unique(df["Elec"], return_inverse=1)
cmap = plt.cm.get_cmap("Set1")

colors= [ palette[i] for i in uind]
fig, ax=plt.subplots(figsize=(15, 5)) 
l = len(df)
pos = np.arange(0,l) % (l//2) + (np.arange(0,l)//(l//2)-1)*0.4

ax.bar(pos, df["Max_Acc"], width=0.4, align="edge", ec="k", color=colors)

handles=[plt.Rectangle((0,0),1,1, color=palette[i], ec="k") for i in range(len(uelec))]

legend=ax.legend(bbox_to_anchor=(0., 1.15, 1., .102), handles=handles, labels=list(uelec),
       prop ={'size':10}, loc=9, ncol=8, title=r'Best algorithm using Max_Acc after undersampling' )

legend.get_frame().set_linewidth(0.0) 
plt.setp(legend.get_title(),fontsize='24')

ax.set_xticks(range(l//2))
ax.set_xticklabels(df["Stage"][:l//2])
ax.set_ylim(0, 110)
ax.get_yaxis().set_visible(False)
ax.spines['top'].set_visible(False) 

#Double x-axis
ax.set_xticks(pos+0.2, minor=True)
clf=df['Clf'].tolist()   
ax.set_xticklabels(clf, minor=True)
plt.setp(ax.get_xticklabels(), rotation=0)
ax.tick_params(axis='x', which='major', pad=25, size=0)

ax=ax 
def annotateBars(row, ax=ax): 
    for p in ax.patches:
        ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                 ha='center', va='center', fontsize=11, color='gray', rotation=90, xytext=(0, 20),
                 textcoords='offset points')  

plot = df.apply(annotateBars, ax=ax, axis=1)