如何从 python 中的 df 绘制置信区间

How to plot confident interval from a df in python

我试图用 .fill_between 将置信区间放入我的图中,但是,标准偏差基于另一列。例如,

我正在绘制一条线

real = df['real'].values
sd = df['S.D'].values
pt.plot(real.index, real.values, color = 'blue')
pt.fill_between(real.index, real.values - (sd*2), real.values + (sd*2),
             color='grey', alpha=0.2)

我做的对吗?输出显示一个奇怪的置信区间(超出 table 范围!)。

很抱歉在您的代码中没有看到这一点。您尝试做事的方式没有问题,但我认为您可能在复制并粘贴到 SO 中的代码中出现了一些拼写错误。以下完美运行:

df = pd.DataFrame({'Real': [3,1,3,3,9,0,4,3,2,3], 
    'S.D.': [0.2,0.5,0.1,1,0.4,0.6,0.8,2,0.3,0.2]}, index=[1,2,3,4,5,6,7,8,9,10])

plt.plot(df.index, df['Real'], color = 'blue')
plt.fill_between(df.index, df['Real'] - (df['S.D.']*2), df['Real'] + (df['S.D.']*2),
             color='grey', alpha=0.2)

plt.show()