Confidence interval of hard coding as a mustache in a line chart

So, I calculated a confident interval for a data set with a normal distribution, and I want to build it like a mustache on a histogram of average data. I tried using the yerr parameter for plt.bar, but it calculates the standard deviation error, not a confident interval. I want the same renderings of the mustache on the barcode screen. I have confident intervals:

[(29600.87, 39367.28), (37101.74, 42849.60), (33661.12, 41470.25), (46019.20, 49577.80)]

Here is my code, I tried to supply yerr parameters with confident levels, but did not work so well.

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))

Here is the plot I get:

enter image description here

+4
source share
2 answers

, ( , , , @ImportanceOfBeingErnest); :

enter image description here

, :

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()
+3

yerr bar . , .. y Âą err. , (y-err, y+err).
; (a, b) y, y-a b-y.

- scalar | N, Nx1 or 2xN array-like. , y , (), 2 x N - .

, .

import numpy as np
import matplotlib.pyplot as plt

# given some mean values and their confidence intervals,
means = np.array([30, 100, 60, 80])
conf  = np.array([[24, 35],[90, 110], [52, 67], [71, 88]])

# calculate the error
yerr = np.c_[means-conf[:,0],conf[:,1]-means ].T
print (yerr) # prints [[ 6 10  8  9]
             #         [ 5 10  7  8]]

# and plot it on a bar chart
plt.bar(range(len(means)), means, yerr=yerr)
plt.xticks(range(len(means)))
plt.show()

enter image description here

0

Source: https://habr.com/ru/post/1672784/


All Articles