在 pandas 的分组条形图中添加误差线 [英] Adding error bars to grouped bar plot in pandas
问题描述
我首先通过生成以下DataFrame在熊猫中生成情节:
I'm generating a plot in pandas by first generating the following DataFrame:
plotData=resultData.groupby(['student_model','lo_id']).describe().nShots.unstack().reset_index()
plotData['se'] = plotData['std']/np.sqrt(plotData['count'])
生成的数据框如下所示:
The resulting dataframe looks like this:
然后我像这样旋转和绘图:
Then I pivot and plot like so:
plotData.pivot(index='student_model',columns='lo_id',values='mean').plot(kind='bar')
结果如下:
这很好,但是我需要将"se"列中的值作为错误栏添加到绘图中,并且无法使其正常工作.我知道我可以添加一个参数来调用图(即...plot(kind='bar', yerr=???)
),但是我不知道如何正确格式化它以使其正常工作.有什么想法吗?
That's all fine, but I need to add the values from the "se" column as errorbars to the plot, and can't get it to work. I know I can add an argument to call to plot (i.e ...plot(kind='bar', yerr=???)
), but I don't know how to properly format this to make it work properly. Any ideas?
推荐答案
将数据作为图片发布不是一个好主意.这是一个解决方案:
Posting data as a picture is not a good idea. Here is a solution:
In [16]:
#se column store the errorbar values
print df
class1 class2 se val
0 A R 1 1
1 A G 1 2
2 B R 1 3
3 B G 1 4
[4 rows x 4 columns]
In [17]:
df.pivot(index='class1',columns='class2',values='val').plot(kind='bar', yerr=df.pivot(index='class1',columns='class2',values='se').values)
#or yerr=df.se.reshape((2,2))
#Where (2,2) is the shape of df.pivot(index='class1',columns='class2',values='val')
#which is less verbose but may not be general
这篇关于在 pandas 的分组条形图中添加误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!