向熊猫条形图添加95%置信区间作为误差线

时间:2019-06-26 16:01:18

标签: python pandas plot confidence-interval errorbar

我想在熊猫条形图中添加95%置信区间误差线,例如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

我查看了熊猫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()

1 个答案:

答案 0 :(得分:1)

给定的链接建议:

./program | tr '\r' '\n'

输出:

enter image description here