Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas to plot barplots with error bars

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?

like image 420
Einar Avatar asked Oct 23 '12 12:10

Einar


1 Answers

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

enter image description here

like image 101
lucasg Avatar answered Sep 19 '22 01:09

lucasg