Calculate and plot 95% confidence interval for a given dataframe

 ̄綄美尐妖づ 提交于 2019-12-11 02:55:18

问题


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:

The dataframe looks like this with 3649 entries:


回答1:


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!



来源:https://stackoverflow.com/questions/42985553/calculate-and-plot-95-confidence-interval-for-a-given-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!