将错误栏添加到pandas中的分组条形图中

mou*_*hio 2 python matplotlib pandas

我首先生成以下DataFrame,在pandas中生成一个图:

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

结果数据框如下所示: 在此输入图像描述

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

plotData.pivot(index='student_model',columns='lo_id',values='mean').plot(kind='bar')
Run Code Online (Sandbox Code Playgroud)

导致以下结果:

在此输入图像描述

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

CT *_*Zhu 6

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

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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 问题解决了!查看 yerr 的值,我认为转置可以完成这项工作。(**yerr.values.T**) (2认同)