Maj*_*din 7 python matplotlib scipy
因此,我计算了一组具有正态分布的数据的置信区间,我想将其绘制为数据均值条形图上的胡须.我尝试对plt.bar使用yerr参数,但它计算标准偏差而不是自信区间.我想在条形图上看到相同的胡须可视化.我有自信的时间间隔:
[(29600.87,39367.28),(37101.74,42849.60),(33661.12,41470.25),(46019.20,49577.80)]
这是我的代码,我尝试用自信的水平提供yerr参数,但效果不是很好.
means=[np.mean(df.iloc[x]) for x in range(len(df.index))]
CI=[st.t.interval(0.95, len(df.iloc[x])-1, loc=np.mean(df.iloc[x]), scale=st.sem(df.iloc[x])) for x in range(len(df.index))]
plt.figure()
plt.bar(x_axis, means, color='r',yerr=np.reshape(CI,(2,4))
plt.xticks(np.arange(1992,1996,1))
Run Code Online (Sandbox Code Playgroud)
这是我得到的情节:
以下应该做你想做的(假设你的错误是对称的;如果不是,那么你应该使用@ImportanceOfBeingErnest 的答案);情节看起来像这样:
生成它的代码带有一些内联注释:
import matplotlib.pyplot as plt
# rough estimates of your means; replace by your actual values
means = [34500, 40000, 37500, 47800]
# the confidence intervals you provided
ci = [(29600.87, 39367.28), (37101.74, 42849.60), (33661.12, 41470.25), (46019.20, 49577.80)]
# get the range of the confidence interval
y_r = [means[i] - ci[i][1] for i in range(len(ci))]
plt.bar(range(len(means)), means, yerr=y_r, alpha=0.2, align='center')
plt.xticks(range(len(means)), [str(year) for year in range(1992, 1996)])
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3433 次 |
最近记录: |