繁体   English   中英

将误差线添加到熊猫的分组条形图中

[英]Adding error bars to grouped bar plot in pandas

我首先通过生成以下DataFrame在熊猫中生成图:

plotData=resultData.groupby(['student_model','lo_id']).describe().nShots.unstack().reset_index()
plotData['se'] = plotData['std']/np.sqrt(plotData['count'])

产生的数据框如下所示: 在此处输入图片说明

然后我像这样旋转和绘图:

plotData.pivot(index='student_model',columns='lo_id',values='mean').plot(kind='bar')

结果如下:

在此处输入图片说明

一切都很好,但是我需要将“ se”列中的值作为错误栏添加到绘图中,并且无法使其正常工作。 我知道我可以添加一个参数来调用图(即...plot(kind='bar', yerr=???) ),但是我不知道如何正确格式化它以使其正常工作。 有任何想法吗?

将数据发布为图片不是一个好主意。 这是一个解决方案:

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM