使用 Pandas 数据框绘制误差线 matplotlib [英] Plotting error bars matplotlib, using pandas data frame

查看:81
本文介绍了使用 Pandas 数据框绘制误差线 matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这相对容易,但我似乎无法让它发挥作用.我想使用matplotlib模块以日期为x轴,以气体为y轴和以std为误差线来绘制此df.我可以使用pandas包装器使其正常工作,但是后来我不知道如何设置错误栏的样式.

I'm sure this is relatively easy, but I can't seem to make it work. I would like to plot this df, with date as the x-axis, gas as the y-axis and std as errorbars using the matplotlib module. I can get it to work using the pandas wrapper, but then I have no idea how to style the errorbars.

使用 pandas matplotlib 包装器

Using pandas matplotlib wrapper

我可以使用 matplotlib pandas 包装器绘制误差线 trip.plot(yerr='std', ax=ax, marker='D') 但是我不知道如何访问错误栏以使用 plt.errorbar()

I can plot the error bars using the matplotlib pandas wrapper trip.plot(yerr='std', ax=ax, marker ='D') But then i'm not sure how to access the error bars to style them like one could in matplotlib using plt.errorbar()

使用Matplotlib

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

The above code throws this error TypeError: unsupported operand type(s) for -: 'float' and 'instancemethod'

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

So basically, what I would like help with is plotting the errorbars using standard matplotlib module rather than the pandas wrapper.

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

推荐答案

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

std is a method on a dataframe, ex df.std().

使用

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

或者如果你有 mpl1.5.0+

or if you have mpl1.5.0+

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

这篇关于使用 Pandas 数据框绘制误差线 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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