使用pandas数据框架绘制误差线matplotlib

2024-06-17 10:16:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我相信这是相对容易的,但我似乎不能让它工作。我想用matplotlib模块绘制这个df,日期是x轴,gas是y轴,std是错误条。我可以使用pandas包装器让它工作,但是我不知道如何设计errorbar。

使用pandas matplotlib包装器

我可以使用matplotlib pandas包装器trip.plot(yerr='std', ax=ax, marker ='D')绘制错误栏,但是我不确定如何访问错误栏,以便像在matplotlib中使用plt.errorbar()那样设置它们的样式

使用Matplotlib

fig, ax = plt.subplots()
ax.bar(trip.index, trip.gas, yerr=trip.std)

或者

plt.errorbar(trip.index, trip.gas, yerr=trip.std)

上面的代码抛出这个错误TypeError: unsupported operand type(s) for -: 'float' and 'instancemethod'

所以基本上,我想要帮助的是使用标准matplotlib模块而不是pandas包装器绘制错误条。

DF==

        date       gas       std
0 2015-11-02  6.805351  7.447903
1 2015-11-03  4.751319  1.847106
2 2015-11-04  2.835403  0.927300
3 2015-11-05  7.291005  2.250171

Tags: 模块pandasdfindexplotmatplotlib错误绘制
1条回答
网友
1楼 · 发布于 2024-06-17 10:16:22

std是数据帧上的方法,例如df.std()

使用

plt.errorbar(trip.index, trip['gas'], yerr=trip['std'])

或者如果你有mpl1.5.0+

plt.errorbar(trip.index, 'gas', yerr='std', data=trip)

相关问题 更多 >