使用数据框中多列的错误条绘制条形图

Plot bar chart with errorbars from multiple columns in a dataframe

我正在尝试做一些应该如此简单但无法通过其他人的类似问题找到答案的事情。我想绘制存储在数据框中的几组数据的条形图,其中错误栏值也存储在数据框中。

我有一个来自商业软件的数据框,它有多个列,我想将其制作成一个簇状条形图,我只能使用 df.plot.bar() 才能正确完成。我现在遇到的问题是我无法弄清楚如何从同一数据框中正确添加错误栏。

这段代码可以很好地从相同格式的样本数据中生成我想要的绘图类型:

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()

如何从相应的 *_SD 列中添加错误栏(只有正数很好,但实际上任何错误栏)?

编辑:问题似乎与我的真实数据框中的行数有关。这是工作和非工作测试代码的示例:

不工作(抛出 ValueError:错误必须是 [标量 | 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()

工作:

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()

已更新以添加 T 以转置 yerr 参数的 np.array。

试试这个:

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)

输出: