pandas ,条形图注释 [英] Pandas, Bar Chart Annotations

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

问题描述

如何正确地为Pandas Bar Charts提供注释?



我正在关注



其中s /他说,


我非常喜欢水平条形图,因为我真的认为他们更容易阅读,但我知道,很多人宁愿看到这个图表实现在一个常规的条形图,这里是代码,你会注意到,为了创建注释*



方案

看起来你的 autolabel 函数期待一个补丁那些条为补丁,我们可以这样做:

  df = pd .DataFrame({'score':np.random.randn(6),
'person':[x * 3 for x in list('ABCDEF')]})

def autolabel(rects):
x_pos = [rect.get_x()+ rect.get_width()/ 2。 for rect in rects]
y_pos = [rect.get_y()+ 1.05 * rect.get_height()for rect in rects]
#if高度常量:hbars,vbars否则
if .diff([rectt.getp(item,'width')for item in rects])== 0).all():
scores = [plt.getp(item,'height') ]
else:
scores = [plt.getp(item,'width')for item in rects]
#附加一些文本标签
用于rect,x,y,s in zip(rects,x_pos,y_pos,scores):
ax.text(x,
y,
'%s'%s,
ha ='center',va = 'bottom')

ax = df.set_index(['person'])。plot(kind ='barh',figsize =(10,7),
color = ['dodgerblue ','slategray'],fontsize = 13)

ax.set_alpha(0.8)
ax.set_title(BarH)#,fontsize = 18)
autolabel .patches)

  ax = df.set_index(['person'])plot(kind ='bar',figsize =(10,7),
color = ['dodgerblue','slategray'],fontsize = 13)

ax.set_alpha(0.8)
ax.set_title(Bar)#,fontsize = 18)
autolabel(ax.patches)


How to properly give Annotations to Pandas Bar Charts?

I'm following Bar Chart Annotations with Pandas and MPL, but somehow I can't make it into my own code -- this is as far as I can go. What's wrong?

I've also found the following code from here:

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

But I don't how to apply that to my code either. Please help.

UPDATE:

Thank you @CT Zhu, for the answer. However, in your horizontal bars, you are still placing the text on top of bars, but I need the text show up within or along them, like this from my referenced article,

where s/he says,

"I am very parital to horizontal bar charts, as I really think they are easier to read, however, I understand that a lot of people would rather see this chart implemented in a regular bar chart. So, here is the code to do that; you will notice that a few things have changed in order to create the annotation"*

解决方案

It appears your autolabel function is expecting a list of patches, sssuming your plot only those bars as its patches, we could do:

df = pd.DataFrame({'score':np.random.randn(6),
                   'person':[x*3 for x in list('ABCDEF')]})

def autolabel(rects):
    x_pos = [rect.get_x() + rect.get_width()/2. for rect in rects]
    y_pos = [rect.get_y() + 1.05*rect.get_height() for rect in rects]
    #if height constant: hbars, vbars otherwise
    if (np.diff([plt.getp(item, 'width') for item in rects])==0).all():
        scores = [plt.getp(item, 'height') for item in rects]
    else:
        scores = [plt.getp(item, 'width') for item in rects]
    # attach some text labels
    for rect, x, y, s in zip(rects, x_pos, y_pos, scores):
        ax.text(x, 
                y,
                '%s'%s,
                ha='center', va='bottom')

ax = df.set_index(['person']).plot(kind='barh', figsize=(10,7), 
              color=['dodgerblue', 'slategray'], fontsize=13)

ax.set_alpha(0.8)
ax.set_title("BarH")#,fontsize=18)
autolabel(ax.patches)

ax = df.set_index(['person']).plot(kind='bar', figsize=(10,7), 
              color=['dodgerblue', 'slategray'], fontsize=13)

ax.set_alpha(0.8)
ax.set_title("Bar")#,fontsize=18)
autolabel(ax.patches)

这篇关于 pandas ,条形图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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