如何使用python在直方图中绘制置信区间条?

时间:2019-02-26 14:00:50

标签: python

我计算了直方图中显示的某些数据的置信区间。 现在,我想在图中添加置信度,但是我不知道该怎么做。有人对我有什么建议吗?

import matplotlib.pyplot as plt
from functools import reduce
plt.style.use('ggplot')
from scipy.stats import sem, t
from scipy import mean
import seaborn as sns

List_slopes= [[0.0437712648015943], [0.03891732777088522], [0.04873966717541519], [0.06834041111023302], [0.03290835239919912], [0.05676673320560569], [0.04193367376660084], [0.05453567515132543],
         [0.0436988106026637], [0.05639531551298649], [0.033151533411499554], [0.05312718258689977], [0.03201472087883465], [0.06138900127756917], [0.04084566472094725], [0.030980452931883645],
         [0.03490031676420862], [0.05617484928592166], [0.05756062483863055], [0.05062138233057321], [0.037458909465997274], [0.0452444541408649], [0.031985252895496544], [0.05202456296370903],
         [0.054154933324027967], [0.052119386421266026], [0.03637328841280627], [0.052831824786532394], [0.05250896675489661], [0.030371934074959257], [0.05559661606514884], [0.0524023454048075]
         , [0.05716250142416688], [0.04272492198584081], [0.05737607811682808], [0.03440778732820213], [0.04386663383861987], [0.0526401870652505], [0.05718775471785554], [0.05862797457176922],
         [0.025116204881676027], [0.05158014618741805], [0.03764036354269462], [0.037389249896867205], [0.062041377418890396], [0.04436736176610321]]

confidence = 0.95

n = len(List_slopes)
average = mean(List_slopes)  # average, mean
print(average)
std_err = sem(List_slopes)  # standard deviation
print(std_err)
confidence_interval = std_err * t.ppf((1 + confidence) / 2, n - 1)  # t.pp is equivalent to Excel TINV(2*0.05,999)
print(confidence_interval)

Lower_CI = average - confidence_interval
Upper_CI = average + confidence_interval

sns.distplot(List_slopes, kde=True)
plt.xlabel("Slopes ")
plt.ylabel("Frequency of Slopes")
plt.text(.5, .75, str(round(confidence * 100)) + '% Confidence Interval', fontsize=25, color='black', ha='center')
plt.text(.5, .35, str(round(Lower_CI[0], 2)) + ' to ' + str(round(Upper_CI[0], 2)), fontsize=25, color='black', ha='center')
plt.show()

在此图中,我把该图来自我的代码。我想绘制0.04和0.05线,因为它们代表误差线的一个实例。我的意思是我想绘制Lower_CI和Upper_CI。

enter image description here

0 个答案:

没有答案
相关问题