Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is pandas applying the same values on both sides of an asymmetric error bar?

I'm trying to plot a series with asymmetric error bars using pandas and matplotlib with the following code:

d = {'high_delta': {1: 0.6,
  2: 0.1,
  3: 0.2,
  4: 0.1,
  5: 0.1,
  6: 0.1,
  7: 0.1,
  8: 0.1,
  9: 0.2,
  10: 0.1},
 'low_delta': {1: 0.2,
  2: 0.1,
  3: 0.1,
  4: 0.1,
  5: 0.1,
  6: 0.1,
  7: 0.1,
  8: 0.1,
  9: 0.1,
  10: 0.4},
 'p_hat': {1: 0.2,
  2: 0.1,
  3: 0.3,
  4: 0.3,
  5: 0.1,
  6: 0.3,
  7: 0.2,
  8: 0.2,
  9: 0.1,
  10: 0.8}}

df = pandas.DataFrame(d)
 df['p_hat'].plot(yerr=df[['low_delta', 'high_delta']].T.values)
(df.p_hat + df.high_delta).plot(style='.')
(df.p_hat - df.low_delta).plot(style='*')

The lower bounds always seem to match what I would expect, but instead of adding the values on the upper bound it seems to be adding the values from the lower bound again.

How should the errors be passed into matplotlib so that the error bars are rendered correctly?

like image 415
2daaa Avatar asked Nov 07 '14 03:11

2daaa


People also ask

Should error bars be symmetrical?

Why do error bars on bar graphs sometimes appear to be asymmetrical? One reason for asymmetrical error bars is because they actually are asymmetrical. If you enter separate up and down error values, they may not be the same. If you plot median with quartiles, the error bars are likely to be asymmetrical.

How do you plot asymmetric error bars?

Asymmetric error bars can be computed from raw data simply by selecting different computations for the upper and lower error bars. However, if you have your error bar data in multiple columns, you will need to select the “Asymmetric Error Bar Columns” option.

How do you add error bars to a plot in Python?

errorbar() method is used to create a line plot with error bars. The two positional arguments supplied to ax. errorbar() are the lists or arrays of x, y data points. The two keyword arguments xerr= and yerr= define the error bar lengths in the x and y directions.


1 Answers

Short answer: Use 1x2xN shaped error lists for asymmetric error bars.

F.ex. in the current example use

errors = [ f.index.values, df['p_hat'].values ]
df['p_hat'].plot(yerr=[errors])

There is currently a bug in Pandas which results in pandas to interpret error bars given in shape 2xN for a series the same way it would interpret multiple error bars for multiple rows of a DataFrame. Since you are obviously plotting only 1 row/series only the first element of the error bars list is used and interpreted as symmetrical errors.

Until the bug is fixed in pandas one can "trick" pandas into using asymmetric errors bars by passing errors in the shape of Mx2xN as is the shape expected for asymmetric error bars on DataFrames. To be precise you have to use a 1x2xN shaped list, which can be simply created by calling f.ex. yerr=[ ... ]

like image 84
ae35 Avatar answered Sep 23 '22 15:09

ae35