Using pandas to plot barplots with error bars

matplotlib, pandas, python

Solution

What is your data shape?

For an n-by-1 data vector, you need a n-by-2 error vector (positive error and negative error):

import pandas as pd 
import matplotlib.pyplot as plt

df2 = pd.DataFrame([0.4, 1.9])
df2.plot(kind='bar', yerr=[[0.1, 3.0], [3.0, 0.1]])

plt.show()

Problem

I'm trying to generate bar plots from a DataFrame like this: ``` Pre Post Measure1 0.4 1.9 ``` These values are median values I calculated from elsewhere, and I have also their variance and standard deviation (and standard error, too). I would like to plot the results as a bar plot with the proper error bars, but specifying more than one error value to `yerr` yields an exception: ``` # Data is a DataFrame instance fig = data.plot(kind="bar", yerr=[0.1, 0.3]) [...] ValueError: In safezip, len(args[0])=1 but len(args[1])=2 ``` If I specify a single value (incorrect) all is fine. How can I actually give each column its correct error bar?

Original source

Related problems