使用 pandas 绘制带有错误条的条形图

编程入门 行业动态 更新时间:2024-10-26 16:22:43
本文介绍了使用 pandas 绘制带有错误条的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从这样的DataFrame生成条形图:

I'm trying to generate bar plots from a DataFrame like this:

Pre Post Measure1 0.4 1.9

这些值是我从别处计算的中值,我也有它们的方差和标准差(以及标准误差).我想将结果绘制为带有正确误差条的条形图,但为 yerr 指定多个错误值会产生异常:

These values are median values I calculated from elsewhere, and I have also their variance and standard deviation (and standard error, too). I would like to plot the results as a bar plot with the proper error bars, but specifying more than one error value to yerr yields an exception:

# Data is a DataFrame instance fig = data.plot(kind="bar", yerr=[0.1, 0.3]) [...] ValueError: In safezip, len(args[0])=1 but len(args[1])=2

如果我指定了一个值(不正确),一切都很好.我怎样才能真正给每列正确的错误栏?

If I specify a single value (incorrect) all is fine. How can I actually give each column its correct error bar?

推荐答案

您的数据形状是什么?

对于 n×1 数据向量,您需要一个 n×2 误差向量(正误差和负误差):

For an n-by-1 data vector, you need a n-by-2 error vector (positive error and negative error):

import pandas as pd import matplotlib.pyplot as plt df2 = pd.DataFrame([0.4, 1.9]) df2.plot(kind='bar', yerr=[[0.1, 3.0], [3.0, 0.1]]) plt.show()

更多推荐

使用 pandas 绘制带有错误条的条形图

本文发布于:2023-07-18 04:31:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   条形图   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!