简体   繁体   English

如何在matplotlib中可视化95%置信区间?

[英]How to visualize 95% confidence interval in matplotlib?

I have learned how to find the 95% confidence interval with scipy.stats.t like so 我已经学会了如何使用scipy.stats.t找到95%的置信区间

In [1]: from scipy.stats import t
In [2]: t.interval(0.95, 10, loc=1, scale=2)  # 95% confidence interval
Out[2]: (-3.4562777039298762, 5.4562777039298762)
In [3]: t.interval(0.99, 10, loc=1, scale=2)  # 99% confidence interval
Out[3]: (-5.338545334351676, 7.338545334351676)

However, visualization is important to me. 但是,可视化对我来说很重要。 I am wondering how may I show the confidence interval bar on each node of my curve in matplotlib ? 我想知道如何在matplotlib曲线的每个节点上显示置信区间条?

What I am expecting is something like this 我期待的是这样的

在此输入图像描述

You don't need .interval method, to get the size of confidence interval, you just need the .ppf method. 你不需要.interval方法来获得置信区间的大小 ,你只需要.ppf方法。

import numpy as np
import scipy.stats as ss
data_m=np.array([1,2,3,4])   #(Means of your data)
data_df=np.array([5,6,7,8])   #(Degree-of-freedoms of your data)
data_sd=np.array([11,12,12,14])   #(Standard Deviations of your data)
import matplotlib.pyplot as plt
plt.errorbar([0,1,2,3], data_m, yerr=ss.t.ppf(0.95, data_df)*data_sd)
plt.xlim((-1,4))

ss.t.ppf(0.95, data_df)*data_sd is a fully vectorize way to get the (half) size of interval, given the degrees of freedom and standard deviation. ss.t.ppf(0.95, data_df)*data_sd是一种完全矢量化的方法,在给定自由度和标准偏差的情况下获得间隔的(一半)大小。

在此输入图像描述

you need to divide by standard deviation, and, second, if your data is two-sided (as plot suggests), you need to allow 2.5% of misses on each side of Gaussian, that is: 你需要除以标准偏差,其次,如果你的数据是双面的(如图所示),你需要在高斯的每一边允许2.5%的未命中,即:

ss.t.ppf(0.975, data_df)/np.sqrt(data_df)

Since you miss 2.5% on both sides, you get total 5% miss. 由于双方都错过了2.5%,所以总共错过了5%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM