- Plotting a grouped bar and the corresponding error bars is dependant upon the shape of the dataframe being passed.
- Use
.pivot
to reshape the dataframe into the correct form to work with yerr
.
- It is a key requirement, that when adding
yerr
as a dataframe, the column headers must be the same as that used for the bars. If the column names are different, the error bars won't show up.
- Tested in
python 3.8.11
, pandas 1.3.3
, matplotlib 3.4.3
import pandas as pd
# dataframe
data = {'class1': ['A', 'A', 'B', 'B'], 'class2': ['R', 'G', 'R', 'G'], 'se': [1, 1, 1, 1], 'val': [1, 2, 3, 4]}
df = pd.DataFrame(data)
class1 class2 se val
0 A R 1 1
1 A G 1 2
2 B R 1 3
3 B G 1 4
# pivot the data
dfp = df.pivot(index='class1', columns='class2', values='val')
class2 G R
class1
A 2 1
B 4 3
# pivot the error
yerr = df.pivot(index='class1', columns='class2', values='se')
class2 G R
class1
A 1 1
B 1 1
# plot
dfp.plot(kind='bar', yerr=yerr, rot=0)
# 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 as generalized
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…