How add asymmetric errorbars to Pandas grouped barplot? How add asymmetric errorbars to Pandas grouped barplot? pandas pandas

How add asymmetric errorbars to Pandas grouped barplot?


After some trial and error I figured it out. As you mentioned:

matplotlib allows for asymmetric errorbars using a yerr=[ylo, yhi] construct

But it turns out that since you have two bar groups (i.e. "2015 2Q" and "2016 1Q"), the shape matplotlib expects is (2, 2, 4), or: [number of groups] x 2 x [number of bars per group].

This is my code, starting after the definition of errLo and errHi in your code:

err = []for col in errLo:  # Iterate over bar groups (represented as columns)    err.append([errLo[col].values, errHi[col].values])err = np.abs(err)  # Absolute error values (you had some negatives)pprint(err)print 'Shape:', np.shape(err)df.pivot(index='ID', columns='quarter', values='Percent').plot(kind='bar', yerr=err)plt.show()

Output:

array([[[ 0.378046,  0.06185 ,  0.866025,  0.028012],        [ 1.776511,  1.123492,  2.645141,  0.82596 ]],       [[ 0.057962,  0.027515,  0.057962,  0.092614],        [ 1.054612,  0.810851,  1.054612,  0.541513]]])Shape: (2L, 2L, 4L)

enter image description here