gpt4 book ai didi

python - 在 pandas 的分组条上绘制误差条

转载 作者:行者123 更新时间:2023-12-02 01:59:54 32 4
gpt4 key购买 nike

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

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'))

enter image description here

正如预期的那样,指数 A 的平均值与同一指数的标准差配对,误差线显示该值的 +/-。

但是,当我尝试在同一个图中绘制“mean1”和“mean2”时,我无法以相同的方式使用标准差:

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'

我最接近我想要的输出是这样的:

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

enter image description here

但是现在误差线不是对称绘制的。相反,每个系列中的绿色和模糊条都使用相同的正负误差,这就是我陷入困境的地方。如何使多系列条形图的误差线具有与只有一个系列时相似的外观?

更新:似乎这已修复在 pandas 0.14 中,我之前正在阅读 0.13 的文档。不过,我现在无法升级我的 Pandas 。稍后再做,看看结果如何。

最佳答案

  • yerr=df[['std1', 'std2']] OP 中的行不起作用,因为列名称与 df[['mean1', 'mean2']] 不同
  • 使用df[['std1', 'std2']].to_numpy().T通过传递没有命名列的错误数组来绕过该问题
  • 测试于python 3.8.11 , pandas 1.3.3 , matplotlib 3.4.3
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'])

mean1 mean2 std1 std2
A 4 6 1 3
B 5 7 5 2

# convert the std columns to an array
yerr = df[['std1', 'std2']].to_numpy().T

# print(yerr)
array([[1, 5],
[3, 2]], dtype=int64)

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

enter image description here

关于python - 在 pandas 的分组条上绘制误差条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144784/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com