在 pandas 中具有多个系列的条形图上绘制误差线 [英] Plotting error bars on barplots with multiple series in pandas

查看:72
本文介绍了在 pandas 中具有多个系列的条形图上绘制误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在单个系列条形图上绘制误差线,如下所示:

I can plot error bars on single series barplots like so:

import pandas as pd
df = pd.DataFrame([[4,6,1,3], [5,7,5,2]], columns = ['mean1', 'mean2', 'std1', 'std2'], index=['A', 'B'])
print(df)
     mean1  mean2  std1  std2
A      4      6     1     3
B      5      7     5     2

df['mean1'].plot(kind='bar', yerr=df['std1'], alpha = 0.5,error_kw=dict(ecolor='k'))

正如预期的那样,将索引A的平均值与相同索引的标准偏差配对,并且误差线显示该值的+/-.

As expected, the mean of index A is paired with the standard deviation of the same index, and the error bar shows the +/- of this value.

但是,当我尝试在同一图中绘制'mean1'和'mean2'时,我不能以相同的方式使用标准偏差:

However, when I try to plot both 'mean1' and 'mean2' in the same plot I cannot use the standard deviations in the same way:

df[['mean1', 'mean2']].plot(kind='bar', yerr=df[['std1', 'std2']], alpha = 0.5,error_kw=dict(ecolor='k'))

    Traceback (most recent call last):

  File "<ipython-input-587-23614d88a3c5>", line 1, in <module>
    df[['mean1', 'mean2']].plot(kind='bar', yerr=df[['std1', 'std2']], alpha = 0.5,error_kw=dict(ecolor='k'))

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\tools\plotting.py", line 1705, in plot_frame
    plot_obj.generate()

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\tools\plotting.py", line 878, in generate
    self._make_plot()

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\tools\plotting.py", line 1534, in _make_plot
    start=start, label=label, **kwds)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\tools\plotting.py", line 1481, in f
    return ax.bar(x, y, w, bottom=start,log=self.log, **kwds)

  File "C:\Users\nameDropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\matplotlib\axes.py", line 5075, in bar
    fmt=None, **error_kw)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\matplotlib\axes.py", line 5749, in errorbar
    iterable(yerr[0]) and iterable(yerr[1])):

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\frame.py", line 1635, in __getitem__
    return self._getitem_column(key)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\frame.py", line 1642, in _getitem_column
    return self._get_item_cache(key)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\generic.py", line 983, in _get_item_cache
    values = self._data.get(item)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\internals.py", line 2754, in get
    _, block = self._find_block(item)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\internals.py", line 3065, in _find_block
    self._check_have(item)

  File "C:\Users\name\Dropbox\Tools\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\pandas\core\internals.py", line 3072, in _check_have
    raise KeyError('no item named %s' % com.pprint_thing(item))

KeyError: u'no item named 0'

我与期望的输出最接近的是:

The closest I have gotten to my desired output is this:

df[['mean1', 'mean2']].plot(kind='bar', yerr=df[['std1', 'std2']].values.T, alpha = 0.5,error_kw=dict(ecolor='k'))

但是现在误差线不是对称绘制的.相反,每个系列中的绿色和模糊条使用相同的正负误差,这就是我遇到的问题.如何使多序列图的误差线的外观与只有一个序列时的误差线相似?

But now the error bars are not plotted symmetrically. Instead the green and blur bars in each series use the same positive and negative error and this is where I am stuck. How can I get the error bars of my multiseries barplot to have a similar appearance as when I had only one series?

更新: 似乎已在 pandas 0.14 <中修复了/a>,我之前阅读过0.13的文档.我现在无法升级我的熊猫.稍后再做,看看结果如何.

Update: Seems like this is fixed in pandas 0.14, I was reading the docs for 0.13 earlier. I don't have the possibility to upgrade my pandas right now though. Will do later and see how it turns out.

推荐答案

让阿让(Ajean)和阿里奥斯(Alios)更加生气!

Roger that Ajean and Alios!

好吧,我终于找到了问题的答案.这是我几天来一直在尝试的事情.在早期版本的Pandas中,该问题显然是一个问题.我安装了Pandas 0.15.0,现在您可以引用另一个数据框,并将数据用于Ceflo试图在上面进行分组的条形图上的误差线.因此,以下代码现在可在Pandas 0.15.0中使用.

Well I did finally find the answer to the question. This is something I've been trying to do for days now. The problem was apparently an issue in an earlier version of Pandas. I installed Pandas 0.15.0 and you can now reference another data frame and use the data for error bars on grouped bar plots like Ceflo was trying to do above. So the following code now works in Pandas 0.15.0.

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([[4,6,1,3], [5,7,5,2]], columns = ['mean1', 'mean2', 'std1', 'std2'], index=['A', 'B'])
print(df)

df[['mean1', 'mean2']].plot(kind='bar', yerr=df[['std1', 'std2']].values.T, alpha = 0.5,error_kw=dict(ecolor='k'))
plt.show()

这篇关于在 pandas 中具有多个系列的条形图上绘制误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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