【发布时间】:2020-08-24 06:14:57
【问题描述】:
首先,很抱歉,如果有人已经问过这个关于这个情节的问题(我已经浏览了几个 stackoverflow 帖子)。
该图有四个平均值条形图,误差范围 (margin=mean +-std*1.96) 由它们顶部的垂直线表示。
无论如何,我遇到的问题是我的条形图在没有 yerr 参数的情况下看起来很好。
但是,一旦我将 yerr 参数 yerr=df.std(axis=1) 引入 plt.bar,平均值条就会以 0 为中心并且很小。看图。
老实说,我不知道问题出在哪里。 我也尝试过使用置信区间进行试验,当引入 yerr 参数时,我仍然得到相同的图。
upper=[1.96*s[i] for i in range(4)], lower=[(-1.96)*s[i] for i in range(4)],
ci=list(zip(lower,upper))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(32000,200000,3650),
np.random.normal(43000,100000,3650),
np.random.normal(43500,140000,3650),
np.random.normal(48000,70000,3650)],
index=[1992,1993,1994,1995])
plt.figure(figsize=(8,8))
xt = [1992,1993,1994,1995]
plt.xticks(xt)
bar_plot = plt.bar(df.index, df.mean(axis=1),capsize=10)
#bar_plot = plt.bar(df.index, df.mean(axis=1),,yerr=df.std(axis=1)*1.96, capsize=10)
plt.show()
【问题讨论】:
标签: pandas dataframe matplotlib bar-chart confidence-interval