如何将非对称错误栏添加到Pandas分组条形图中? [英] How add asymmetric errorbars to Pandas grouped barplot?

查看:105
本文介绍了如何将非对称错误栏添加到Pandas分组条形图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循此问题的答案我可以为自己的数据重现相同的结果.但是,我需要绘制不对称错误条.

Following the accepted answer to this question I am able to reproduce the same results for my own data. However, I need to plot asymmetric error bars.

dfdict = {'ID': ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'],
      'quarter': ['2015 2Q', '2016 1Q', '2015 2Q', '2016 1Q', '2015 2Q',
                  '2016 1Q', '2015 2Q', '2016 1Q'],
      'Percent': [0.851789, 0.333333, 0.355240, 0.167224, 1.533220,
                  0.333333, 0.170358, 0.000000],
      'AgrCoullLower': [ 0.378046, 0.057962,  0.061850, -0.027515,
                         0.866025, 0.057962, -0.028012, -0.092614],
      'AgrCoullUpper': [1.776511, 1.054612, 1.123492, 0.810851,
                        2.645141, 1.054612, 0.825960, 0.541513]}
df = pd.DataFrame(dfdict)
df
  ID  quarter   Percent  AgrCoullLower  AgrCoullUpper
0  A  2015 2Q  0.851789       0.378046       1.776511
1  A  2016 1Q  0.333333       0.057962       1.054612
2  B  2015 2Q  0.355240       0.061850       1.123492
3  B  2016 1Q  0.167224      -0.027515       0.810851
4  C  2015 2Q  1.533220       0.866025       2.645141
5  C  2016 1Q  0.333333       0.057962       1.054612
6  D  2015 2Q  0.170358      -0.028012       0.825960
7  D  2016 1Q  0.000000      -0.092614       0.541513

errLo = df.pivot(index='ID', columns='quarter', values='AgrCoullLower')
errHi = df.pivot(index='ID', columns='quarter', values='AgrCoullUpper')

df.pivot(index='ID', columns='quarter', values='Percent')\
    .plot(kind='bar', yerr=errLo)

由于matplotlib使用yerr=[ylo, yhi]构造允许不对称的误差线,我希望在这里有类似的东西.不幸的是,无法简单地替换yerr=[errLo, errHi],因为不能简单地以这种方式插入数组(形状为(4,2))(稍后再进行堆栈跟踪).使用yerr=np.column_stack((errLo, errHi)),该图包含对称误差线(永远不要使用第二个数组值).使用yerr=np.row_stack((errLo, errHi)),我得到

As matplotlib allows for asymmetric errorbars using a yerr=[ylo, yhi] construct, I was hoping for something similar here. Unfortunately, it is not possible to simply substitute yerr=[errLo, errHi] since the arrays (having shape (4,2)) cannot be simply inserted this way (stack trace later), Using yerr=np.column_stack((errLo, errHi)) the plot contains symmetric error bars (never using the second array values). Using yerr=np.row_stack((errLo, errHi)), I get

ValueError: yerr must be a scalar, the same dimensions as y, or 2xN.

有没有办法哄骗熊猫提供分组的,不对称错误栏?

Is there a way to cajole Pandas into providing grouped, asymmetric errorbars?

该堆栈跟踪:

ValueError                                Traceback (most recent call last)
<ipython-input-26-336a22db15e6> in <module>()
----> 1 df.pivot(index='ID', columns='quarter', values='Percent')    .plot(kind='bar', yerr=[errLo, errHi])

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   3669                           fontsize=fontsize, colormap=colormap, table=table,
   3670                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3671                           sort_columns=sort_columns, **kwds)
   3672     __call__.__doc__ = plot_frame.__doc__
   3673 

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2554                  yerr=yerr, xerr=xerr,
   2555                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 2556                  **kwds)
   2557 
   2558 

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   2380                             pass
   2381                 data = series
-> 2382         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   2383 
   2384     plot_obj.generate()

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in __init__(self, data, **kwargs)
   1843 
   1844         self.log = kwargs.pop('log',False)
-> 1845         MPLPlot.__init__(self, data, **kwargs)
   1846 
   1847         if self.stacked or self.subplots:

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in __init__(self, data, kind, by, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, fig, title, xlim, ylim, xticks, yticks, sort_columns, fontsize, secondary_y, colormap, table, layout, **kwds)
    904         self.errors = {}
    905         for kw, err in zip(['xerr', 'yerr'], [xerr, yerr]):
--> 906             self.errors[kw] = self._parse_errorbars(kw, err)
    907 
    908         if not isinstance(secondary_y, (bool, tuple, list, np.ndarray, Index)):

/usr/local/lib/python3.4/dist-packages/pandas/tools/plotting.py in _parse_errorbars(self, label, err)
   1423             else:
   1424                 # raw error values
-> 1425                 err = np.atleast_2d(err)
   1426 
   1427             err_shape = err.shape

/usr/local/lib/python3.4/dist-packages/numpy/core/shape_base.py in atleast_2d(*arys)
     98     res = []
     99     for ary in arys:
--> 100         ary = asanyarray(ary)
    101         if len(ary.shape) == 0:
    102             result = ary.reshape(1, 1)

/usr/local/lib/python3.4/dist-packages/numpy/core/numeric.py in asanyarray(a, dtype, order)
    523 
    524     """
--> 525     return array(a, dtype, copy=False, order=order, subok=True)
    526 
    527 def ascontiguousarray(a, dtype=None):

ValueError: cannot copy sequence with size 4 to array axis with dimension 2

推荐答案

经过反复试验,我发现了这一点.如您所述:

After some trial and error I figured it out. As you mentioned:

matplotlib允许使用yerr=[ylo, yhi]的不对称错误栏 构造

matplotlib allows for asymmetric errorbars using a yerr=[ylo, yhi] construct

但是事实证明,由于您有两个钢筋组(即"2015 2Q"和"2016 1Q"),因此matplotlib期望的形状为(2,2,4),或:[组数] x 2 x [每组的酒吧数量].

But it turns out that since you have two bar groups (i.e. "2015 2Q" and "2016 1Q"), the shape matplotlib expects is (2, 2, 4), or: [number of groups] x 2 x [number of bars per group].

这是我的代码,从代码中的errLo和errHi定义开始:

This is my code, starting after the definition of errLo and errHi in your code:

err = []
for col in errLo:  # Iterate over bar groups (represented as columns)
    err.append([errLo[col].values, errHi[col].values])
err = np.abs(err)  # Absolute error values (you had some negatives)
pprint(err)
print 'Shape:', np.shape(err)

df.pivot(index='ID', columns='quarter', values='Percent').plot(kind='bar', yerr=err)
plt.show()

输出:

array([[[ 0.378046,  0.06185 ,  0.866025,  0.028012],
        [ 1.776511,  1.123492,  2.645141,  0.82596 ]],

       [[ 0.057962,  0.027515,  0.057962,  0.092614],
        [ 1.054612,  0.810851,  1.054612,  0.541513]]])
Shape: (2L, 2L, 4L)

这篇关于如何将非对称错误栏添加到Pandas分组条形图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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