使用pandas数据框绘制错误条matplotlib

hse*_*bie 7 python plot matplotlib pandas

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

使用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)
Run Code Online (Sandbox Code Playgroud)

要么

plt.errorbar(trip.index, trip.gas, yerr=trip.std)
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出了这个错误 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
Run Code Online (Sandbox Code Playgroud)

tac*_*ell 12

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

使用

plt.errorbar(trip.index, trip['gas'], yerr=trip['std'])
Run Code Online (Sandbox Code Playgroud)

或者如果你有mpl1.5.0 +

plt.errorbar(trip.index, 'gas', yerr='std', data=trip)
Run Code Online (Sandbox Code Playgroud)

  • 我知道这是一个超级旧的线程和东西,但是使用 df.std() 作为估计器确实不是一个好主意。它们通常不相同。相反,请使用 df.sem()。通常人们想要平均值的标准误差而不是标准差。这可能是错误处理中最常见的错误,并且在许多出版物中都是错误的。 (14认同)