使用 pandas 绘制带有错误条的条形图 [英] Using pandas to plot barplots with error bars

查看:46
本文介绍了使用 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 绘制带有错误条的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆