为什么 pandas 在不对称误差线的两边都应用相同的值? [英] Why is pandas applying the same values on both sides of an asymmetric error bar?

查看:138
本文介绍了为什么 pandas 在不对称误差线的两边都应用相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pandas和matplotlib通过以下代码绘制具有不对称误差线的序列:

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.

应如何将错误传递到matplotlib中,以便正确显示错误栏?

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

推荐答案

简短答案:对非对称误差线使用1x2xN形状的误差列表.

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

F.ex.在当前示例中,使用

F.ex. in the current example use

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


Pandas中当前存在一个错误,该错误会导致Pandas解释一系列形状为2xN的错误条,其解释方式与解释DataFrame的多行的多个错误条的方式相同.由于显然只绘制了1行/系列,所以仅使用误差线列表的第一个元素并将其解释为对称误差.


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.

在熊猫中修复了 bug 之前,人们可以欺骗"熊猫使用非对称熊猫通过传递Mx2xN形状的错误来传递错误条,就像DataFrames上非对称错误条所期望的形状一样.准确地说,您必须使用1x2xN形状的列表,可以通过调用f.ex轻松创建该列表. yerr=[ ... ]

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

这篇关于为什么 pandas 在不对称误差线的两边都应用相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆