在Pandas上用值注释条形图(在Seaborn因子图条形图上) [英] Annotate bars with values on Pandas (on Seaborn factorplot bar plot)

查看:86
本文介绍了在Pandas上用值注释条形图(在Seaborn因子图条形图上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码来尝试解决这个问题: https://stackoverflow. com/questions/39477748/如何在熊猫上标注值在季节性因素图上显示熊猫图

I wrote some code to try and solve this question: https://stackoverflow.com/questions/39477748/how-to-annotate-bars-with-values-on-pandas-on-seaborn-factorplot-bar-plot

我使用了可以在这里找到的部分代码: matplotlib高级条形图

I used part of the code that can be found here: matplotlib advanced bar plot

为什么图表这么小?该代码只是告诉您从Pandas数据框中获取准确性.

Why is the graph so small? The code just tells to grab the accuracies from 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轴"进行分组:(

This is code to annotate each bar, but ...with Pandas and Matplotlib. The only problem is that I do not know how to change colors and group the "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)

这篇关于在Pandas上用值注释条形图(在Seaborn因子图条形图上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆