1
$\begingroup$

For the following dataframe, I am trying to plot the means of a sample of 5 random rows . And also plot their respective confidence intervals using errorbars. I am unable to figure how to plot the confidence intervals using errorbars.

  col0  col1  col2  col3  col4  col5  col6  col7
0     0     1     2     3     4     5     6     7
1     8     9    10    11    12    13    14    15
2    16    17    18    19    20    21    22    23
3    24    25    26    27    28    29    30    31
4    32    33    34    35    36    37    38    39
5    40    41    42    43    44    45    46    47
6    48    49    50    51    52    53    54    55
7    56    57    58    59    60    61    62    63

Here is my code:

confidenceInterval_percent = 0.95
sample_size=5
mean_list=[]
df = pd.DataFrame(np.arange(64).reshape(8,8)).add_prefix('col')
sample_df= df.sample(n=sample_size, random_state=10)
for i in df.columns:
    mean = df[i].mean()
    mean_list.append(mean)
    stdError = df[i].std() / np.sqrt(sample_size)
    confidence_intervals = sp.stats.norm.interval(
                        confidenceInterval_percent, loc = mean, 
                           scale = stdError)
    temporary_Df=pd.DataFrame(mean_list)
    temporary_Df.plot.bar()
    plt.show()
$\endgroup$

1 Answer 1

0
$\begingroup$

For a confidence level of 95%, you can calculate the mean and sample standard deviation, then find the bands of your interval with: $\bar{x} \pm 1.96*\frac{s}{\sqrt{n}}$

By creating an array with each pair of values for each sample, you can then plot them on your chart.

Matplotlib has a great method for adding error bars, check it out: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.errorbar.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.