首页 > 解决方案 > 使用 matplotlib 将误差线应用于堆叠直方图

问题描述

我有手动计算的(系统)误差线,我想把它放在堆叠直方图的顶部。

我已经能够成功地为常规(非堆叠)直方图箱做到这一点。

我不确定如何准确调用堆叠直方图的 bin 中心和 bin 值,以便能够在顶部绘制误差线。我这样做对吗?如何将误差条放在堆叠箱的顶部?

这是 jupyter 笔记本代码:

fig = plt.figure(figsize=(12,7))
BINS = np.linspace(0,10,11)

xstacked = [df_Event_beam_off['N_sps50'].values,df_Event_overlay['N_sps50'].values]

LABELS = ['Off Beam','Overlay']


plt.hist(xstacked,bins=BINS,histtype='stepfilled',label=LABELS,weights=WEIGHTS_Event_neutrino,stacked=True)




vals_ON_stat,bine_ON_stat = np.histogram(df_Event_beam_on['N_sps50'].values,bins=BINS,weights=weightsON_Event_neutrino)
binc_ON_stat = 0.5*(bine_ON_stat[1:]+bine_ON_stat[:-1])
vals_ON_stat = vals_ON_stat.astype(float)
errs_ON_stat = np.sqrt(vals_ON_stat)


total_error_stacked= np.sqrt((errs_Off_stat)**2+(errs_Overlay_stat)**2+(bin_diff_overlay_syst*vals_Overlay_stat)**2)

plt.errorbar(binc_ON_stat,vals_ON_stat,yerr=errs_ON_stat,fmt='o',color='k',markersize=5,label='ON Beam')

plt.grid()
plt.show()

现在,我希望能够为堆叠做类似的事情,如下所示:

plt.errorbar(binc_stacked,vals_stacked,yerr=total_error_stacked,fmt='o',markersize=5,label='Total Error')

这显然会给出一个错误,说明它不能以不同的尺寸/形状进行广播。有什么建议么?

标签: pythonmatplotlib

解决方案


推荐阅读