简体   繁体   English

使用数据框中多个列的误差条来绘制条形图

[英]Plot bar chart with errorbars from multiple columns in a dataframe

I am trying to do something that should be so simple but cannot find an answer via other's similar questions. 我正在尝试做的事情应该很简单,但无法通过其他人的类似问题找到答案。 I want to plot a bar graph of several groups of data that are stored in a dataframe with errorbar values that are also stored in the dataframe. 我想绘制存储在数据框中的几组数据的条形图,同时也存储在数据框中的误差线值。

I have a dataframe that is coming from commercial software that has multiple columns I'd like to make into a clustered bar graph which I have only managed to do properly using df.plot.bar(). 我有一个来自商业软件的数据框,该数据框具有多个列,我想将它们做成一个聚集的条形图,而我只能使用df.plot.bar()正确地进行处理。 The issue I'm having now is just that I cannot figure out how to add error bars correctly from the same dataframe. 我现在遇到的问题只是我无法弄清楚如何从同一数据帧正确添加错误栏。

This code works fine to generate the type of plot I want from sample data in the same format: 这段代码可以很好地从相同格式的样本数据生成我想要的绘图类型:

df = pd.DataFrame()

#the groups can vary 
grp1 = 'a'
grp2 = 'b'
grp3 = 'c'

df['label'] = ['ID_1','ID_2','ID_3']
df[grp1+'_int'] = [5,5.5,6]
df[grp1+'_SD'] = [1,2,3]
df[grp2+'_int'] = [7,6,5]
df[grp2+'_SD'] = [2,1,1.5]
df[grp3+'_int'] = [6.5,5,5.5]
df[grp3+'_SD'] = [1.5,1.5,2]

ax = df.plot.bar(x='label', y=[grp1+'_int',grp2+'_int',grp3+'_int'])
plt.show()

How can I add errorbars (positive only is fine, but really any errorbars) from the corresponding *_SD columns? 如何从相应的* _SD列添加错误栏(仅正值可以,但实际上任何错误栏)?

Edit: the issue seems to be related to the number of rows in my real dataframe. 编辑:问题似乎与实际数据框中的行数有关。 Here is an example of a working and non-working test code: 这是一个有效和无效的测试代码的示例:

Not Working (throws ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]): 不起作用(引发ValueError:err必须为[标量| N,Nx1或2xN类似于数组的数组]):

df = pd.DataFrame()

#the groups can vary 
grp1 = 'a'
grp2 = 'b'
grp3 = 'c'

df['label'] = ['ID_1','ID_2','ID_3','ID_4']
df[grp1+'_int'] = np.linspace(1,10,4)
df[grp1+'_SD'] = np.linspace(1,2,4)
df[grp2+'_int'] = np.linspace(2,8,4)
df[grp2+'_SD'] = np.linspace(1.5,3,4)
df[grp3+'_int'] = np.linspace(0.5,9,4)
df[grp3+'_SD'] = np.linspace(1,8,4)
print(df)
ax = df.plot.bar(x='label', y=[grp1+'_int',grp2+'_int',grp3+'_int'], yerr=df[[grp1+'_SD', grp2+'_SD', grp3+'_SD']].values)
plt.show()

Working: 工作方式:

df = pd.DataFrame()

#the groups can vary 
grp1 = 'a'
grp2 = 'b'
grp3 = 'c'

df['label'] = ['ID_1','ID_2','ID_3']
df[grp1+'_int'] = np.linspace(1,10,3)
df[grp1+'_SD'] = np.linspace(1,2,3)
df[grp2+'_int'] = np.linspace(2,8,3)
df[grp2+'_SD'] = np.linspace(1.5,3,3)
df[grp3+'_int'] = np.linspace(0.5,9,3)
df[grp3+'_SD'] = np.linspace(1,8,3)
print(df)
ax = df.plot.bar(x='label', y=[grp1+'_int',grp2+'_int',grp3+'_int'], yerr=df[[grp1+'_SD', grp2+'_SD', grp3+'_SD']].values)
plt.show()

Updated to add T to transpose the np.array for yerr parameter. 更新以添加T来将yerr参数的np.array转置。

Try this: 尝试这个:

df = pd.DataFrame()

#the groups can vary 
grp1 = 'a'
grp2 = 'b'
grp3 = 'c'

df['label'] = ['ID_1','ID_2','ID_3']
df[grp1+'_int'] = [5,5.5,6]
df[grp1+'_SD'] = [1,2,3]
df[grp2+'_int'] = [7,6,5]
df[grp2+'_SD'] = [2,1,1.5]
df[grp3+'_int'] = [6.5,5,5.5]
df[grp3+'_SD'] = [1.5,1.5,2]

ax = df.plot.bar(x='label', 
                y=[grp1+'_int',grp2+'_int',grp3+'_int'],
                yerr=df[['a_SD','b_SD','c_SD']].T.values)

Output: 输出:

在此处输入图片说明

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

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