Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

matplotlib - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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=[ ... ]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...