用 Pandas 上的值注释条形图(在 Seaborn factorplot 条形图上)

Annotate bars with values on Pandas (on Seaborn factorplot bar plot)

我写了一些代码来尝试解决这个问题:

我使用了可在此处找到的部分代码: matplotlib advanced bar plot

为什么图表这么小?该代码只是告诉我们从 Pandas dataframe 中获取准确度。

代码:

sns.set(style="white")
g = sns.factorplot(x="Stages", y="Accuracy", hue="Dataset", data=df, saturation = 5, size=4, aspect=2, kind="bar",
              palette= myPalette, legend=False)

ax=g.ax
def annotateBars(row, ax=ax):
    if row['Accuracy'] < 20:
        color = 'white'
        vertalign = 'bottom'
        vertpad = 2
    else:
        color = 'black'
        vertalign = 'top'
        vertpad = -2

    ax.text(row.name, row['Accuracy'] + vertpad, "{:.1f}%".format(row['Accuracy']),
            zorder=10, rotation=90, color=color,
            horizontalalignment='center',
            verticalalignment=vertalign,
            fontsize=12, weight='heavy')

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

这是注释每个条形的代码,但是...使用 Pandas 和 Matplotlib。唯一的问题是我不知道如何更改颜色和分组 "x axis" :(

    df = df.set_index('Stages')
    ax = df.plot.bar(title="Accuracy")
    ax.set_ylim(0, 120)
    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', rotation=90, xytext=(0, 20), textcoords='offset points')  #vertical bars

    #Seaborn --factorplot

    colors = ["windows blue", "orange red", "grey", "amber"]  
    myPalette = sns.xkcd_palette(colors) #envío "colors" a la función xkcd_palette

    sns.set(style="white") #fondo blanco
    g = sns.factorplot(x="Stages", y="Accuracy", hue="Dataset", data=df, saturation=5, size=4, aspect=3, kind="bar",
              palette= myPalette, legend=False) #se suprime la leyenda

    g.set(ylim=(0, 140)) 
    g.despine(right=False) 
    g.set_xlabels("") 
    g.set_ylabels("")  
    g.set_yticklabels("") 


   #Matplotlib --legend creation

     myLegend=plt.legend(bbox_to_anchor=(0., 1.2, 1., .102), prop ={'size':10}, loc=10, ncol=4,  #left, bottom, width, height
                title=r'TOTAL ACCURACY AND PER STAGE-RANDOM FOREST')                    
     myLegend.get_title().set_fontsize('24')



     #Matplotlib --anotación de barras

       ax=g.ax #annotate axis = seaborn axis
       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')  verticales


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

现在可以更简洁地绘制此图

  1. Axes.bar_label automatically labels bars

    for container in ax.containers:
        ax.bar_label(container)
    
  2. Axes.legend 包括 fontsizetitle_fontsize 参数 [自 matplotlib 3.0 起]

    ax.legend(fontsize=10, title='ACCURACY', title_fontsize=24)
    
  3. 另请注意,seaborn.factorplot 已重命名为 seaborn.catplot [自 seaborn 0.9]


已更新seaborn.catplot

colors = ['xkcd:windows blue', 'xkcd:orange red', 'xkcd:grey', 'xkcd:amber']  
g = sns.catplot(x='Stages', y='Accuracy', hue='Dataset', data=df,
                kind='bar', height=4, aspect=3, palette=colors, legend=False)

# auto-label bars
for container in g.ax.containers:
    g.ax.bar_label(container, fmt='%.2f', padding=2, rotation=90)

# add legend with custom font sizes
ax.legend(bbox_to_anchor=(0, 1.2, 1, 0.102), loc=10, ncol=4, fontsize=10,
          title='TOTAL ACCURACY AND PER STAGE-RANDOM FOREST', title_fontsize=24)

# redecorate
g.despine(right=False)
g.set_xlabels('')
g.set_ylabels('')
g.ax.set_yticklabels([])

已更新DataFrame.plot.bar

ax = df.pivot('Stages', 'Dataset', 'Accuracy').plot.bar(legend=False)

# auto-label bars
for container in ax.containers:
    ax.bar_label(container, fmt='%.2f', padding=3, rotation=90, size='small')

# add legend with custom font sizes
ax.legend(bbox_to_anchor=(0, 1.1, 1, 0.102), loc=10, ncol=4, fontsize='small',
          title='TOTAL ACCURACY AND PER STAGE-RANDOM FOREST', title_fontsize='xx-large')

# redecorate
sns.despine(right=False)
ax.set_yticklabels([])
plt.xticks(rotation=0)