python - 将 95% 置信区间作为误差线添加到 pandas 条形图中

标签 python pandas plot confidence-interval errorbar

我想将 95% 置信区间误差线添加到 pandas 条形图中,例如 here 。这是我的数据的样子:

ciRatings.head(20)

                            count   mean        std
condition   envCond         
c01         CSNoisyLvl1     40      4.875000    0.404304
            CSNoisyLvl2     40      4.850000    0.361620
            LabNoisyLvl1    52      4.826923    0.382005
            LabNoisyLvl2    52      4.826923    0.430283
            LabQuiet        92      4.826087    0.408930
c02         CSNoisyLvl1     40      2.825000    0.902631
            CSNoisyLvl2     40      3.000000    0.816497
            LabNoisyLvl1    52      3.250000    1.218726
            LabNoisyLvl2    52      3.096154    1.089335
            LabQuiet        92      2.956522    1.036828
c03         CSNoisyLvl1     40      3.750000    0.669864
            CSNoisyLvl2     40      3.775000    0.659740
            LabNoisyLvl1    52      4.307692    0.728643
            LabNoisyLvl2    52      4.288462    0.723188
            LabQuiet        92      3.967391    0.790758
c06         CSNoisyLvl1     40      4.450000    0.638508
            CSNoisyLvl2     40      4.250000    0.669864
            LabNoisyLvl1    52      4.692308    0.578655
            LabNoisyLvl2    52      4.384615    0.599145
            LabQuiet        92      4.717391    0.452735

我看了看 Pandas documentation关于如何使用错误栏,并尝试复制他们的代码示例。我想出了以下内容:

# calculate range of CI around mean (as it is symmetric)
ci95_lower = []

for i in ciRatings.index:
    count, mean, std = ciRatings.loc[i]
    ci95_lower.append(mean - 1.96*std/math.sqrt(count))

ciRatings['CI95_lower'] = ci95_lower
ciRatings['CI95_range'] = ciRatings['mean'] - ciRatings['CI95_lower']

# extract CI range and means
ciRange = ciRatings[['CI95_range']]
ciRange = ciRange.unstack()
ciRatings = ciRatings[['mean']]

# bar plot with CI95 as error lines
ciBarPlot = ciRatings.unstack().plot(kind='bar', yerr=ciRange, capsize=4)

plt.show()

但是,这会产生下图,显然没有误差线。我的错误是什么?我认为我误解了我到底必须将绘图函数作为 yerr 参数传递的内容。

bar plot

编辑:使用 Quang Hoang 的答案,我按如下方式更改了代码以实现所需的置信区间条:

# calculate range of CI around mean (as it is symmetric)
ci95_lower = []

for i in ciRatings.index:
    count, mean, std = ciRatings.loc[i]
    ci95_lower.append(mean - 1.96*std/math.sqrt(count))

ciRatings['CI95_lower'] = ci95_lower
ciRatings['CI95_range'] = ciRatings['mean'] - ciRatings['CI95_lower']

# bar plot with CI95 lines
ciBarPlot = ciRatings['mean'].unstack(level=1).plot.bar(
            yerr=ciRatings['CI95_range'].unstack(level=1), capsize=4)

plt.show()

最佳答案

给定的链接建议:

fig, ax = plt.subplots(figsize=(12,8))
(df['mean'].unstack(level=1)
           .plot.bar(yerr=df['std'].unstack(level=1) * 1.96,
                     ax=ax, capsize=4)
)
plt.show()

输出:

enter image description here

关于python - 将 95% 置信区间作为误差线添加到 pandas 条形图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776919/

相关文章:

python - 计算二维数组中出现的次数

python - AWS Lambda Python 与 MySQL

python - 使用不同的连接列外连接 Spark 数据框,然后合并连接列

python - pandas:按出现顺序排序

matlab - 与图上的移动线同步播放音频文件,MATLAB

python - 将 pandas 数据框压缩到一个新的数据框中

python - 如何在组内逐条查找唯一值?

pandas - 使用 pandas 从 AWS S3 读取 Parquet 文件

r - 如何在绘图区域外的 geom_text() 中更改字体大小?

python - 如何在 MatPlotLib (NumPy) 中绘制多维数据的轮廓?