使用 Pandas 数据框绘制误差线 matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35562556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:44:18  来源:igfitidea点击:

Plotting error bars matplotlib, using pandas data frame

pythonpandasmatplotlibplot

提问by hselbie

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.

我确信这相对容易,但我似乎无法让它工作。我想使用 matplotlib 模块绘制这个 df,日期为 x 轴,gas 为 y 轴,std 作为误差条。我可以使用 Pandas 包装器让它工作,但是我不知道如何设置错误栏的样式。

Using pandas matplotlib wrapper

使用Pandas matplotlib 包装器

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 pandas 包装器绘制误差线trip.plot(yerr='std', ax=ax, marker ='D')但是我不确定如何访问误差线来设置它们的样式,就像在 matplotlib 中使用plt.errorbar()

Using Matplotlib

使用 Matplotlib

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

or

或者

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

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

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

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

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

DF ==

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

回答by tacaswell

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

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

Use

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

or if you have mpl1.5.0+

或者如果你有 mpl1.5.0+

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