使用 Pandas 绘制带有误差条的条形图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13030488/
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
Using pandas to plot barplots with error bars
提问by Einar
I'm trying to generate bar plots from a DataFrame like this:
我正在尝试从这样的 DataFrame 生成条形图:
Pre Post
Measure1 0.4 1.9
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 yerryields an exception:
这些值是我从别处计算的中值,我也有它们的方差和标准偏差(以及标准误差)。我想将结果绘制为带有正确误差条的条形图,但指定多个错误值会yerr产生异常:
# 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?
如果我指定一个值(不正确),一切都很好。我如何才能真正为每一列提供正确的误差线?
采纳答案by lucasg
What is your data shape?
你的数据形状是什么?
For an n-by-1 data vector, you need a n-by-2 error vector (positive error and negative error):
对于 n×1 数据向量,您需要一个 n×2 误差向量(正误差和负误差):
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()


