python - 使用 pandas dataframe 绘制误差线 matplotlib

标签 python pandas matplotlib plot

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

使用 Pandas matplotlib 包装器

我可以使用 matplotlib pandas wrapper trip.plot(yerr='std', ax=ax, marker ='D') 绘制误差线但是我不确定如何使用 plt.errorbar()

访问错误栏以像在 matplotlib 中那样设置它们的样式

使用 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

最佳答案

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

使用

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

或者如果你有 mpl1.5.0+

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

关于python - 使用 pandas dataframe 绘制误差线 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35562556/

相关文章:

python - 将 requests.Session() cookie 传输到 Python 中的 selenium 网络驱动程序

python Pandas : applying different aggregate functions to different columns

python - SMOTE - 无法将字符串转换为 float

python - Matplotlib:如何在 3D 中高效地绘制大量线段?

python - matplotlib 在 tkinter Canvas 中的缩放功能

python - 如何在 matplotlib 的主图外绘制箭头和矩形(用于蛋白质 sec 结构)?

python - 在 matplotlib 中绘制来自 librosa 的音频

python - 使用不同颜色的切片图 - 手动重新着色条形

python - 安装pygame时出错

Python Panda read_excel - 用户输入部分 io 名称