Sourav
Sourav

Reputation: 69

Calculate and plot 95% confidence interval for a given dataframe

How do I plot a bar plot from a data frame and a 95% confidence interval in the same graph using matplotlib on python(using the yerr arguement if possible). The plot should look like: Plot

The dataframe looks like this with 3649 entries: dataframe(df)

Upvotes: 3

Views: 9824

Answers (1)

Dani Opitz
Dani Opitz

Reputation: 43

Try this:

import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

mean = df.mean(axis = 1)
std = df.std(axis = 1)

n= df.shape[1]
yerr = std / np.sqrt(n) * stats.t.ppf(1-0.05/2, n - 1)

plt.figure()
plt.bar(range(df.shape[0]), mean, yerr = yerr) 
plt.show() 

Good luck!

Upvotes: 3

Related Questions