如何可视化matplotlib中的95%置信区间? [英] How to visualize 95% confidence interval in matplotlib?

查看:37
本文介绍了如何可视化matplotlib中的95%置信区间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学会了如何使用 scipy.stats.t 找到 95% 的置信区间

I have learned how to find the 95% confidence interval with scipy.stats.t like so

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)

然而,可视化对我来说很重要.我想知道如何在 matplotlib 中曲线的每个节点上显示置信区间条?

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?

我期待的是这样的

推荐答案

你不需要 .interval 方法,来获得置信区间的大小,你只需要需要 .ppf 方法.

You don't need .interval method, to get the size of confidence interval, you just need the .ppf method.

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 是在给定自由度和标准偏差的情况下获得区间(半)大小的完全矢量化方法.

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.

这篇关于如何可视化matplotlib中的95%置信区间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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