具有加权平均趋势线的Python 2d比率图

时间:2020-02-29 17:07:11

标签: python numpy matplotlib data-visualization seaborn

您好,谢谢。我从一个熊猫数据帧开始,我想制作一条带有趋势线的二维图,其中显示了加权平均值y值,并带有平均值误差的误差线。均值应按每个仓中事件的总数加权。我首先将df分为“光子”组和“总计”组,其中“光子”是总数的子集。在每个单元中,我正在绘制光子事件占总数的比率。在x轴和y轴上,我有两个不相关的变量“集群能量”和“周长能量”。 我的尝试:

#make the 2d binning and total hist
energybins=[11,12,13,14,15,16,17,18,19,20,21,22]
ybins = [0,.125,.25,.5,.625,.75,1.,1.5,2.5]
total_hist,x,y,i = plt.hist2d(train['total_energy'].values,train['max_perimeter'].values,[energybins,ybins])
total_hist = np.array(total_hist)
#make the photon 2d hist with same bins
groups = train.groupby(['isPhoton'])
prompt_hist,x,y,i = plt.hist2d(groups.get_group(1)['total_energy'].values,groups.get_group(1)['max_perimeter'].values,bins=[energybins,ybins])
prompt_hist = np.array(prompt_hist)
ratio = np.divide(prompt_hist,total_hist,out=np.zeros_like(prompt_hist),where = total_hist!=0)
#plot the ratio
fig, ax = plt.subplots()
ratio=np.transpose(ratio)
p = ax.pcolormesh(ratio,)
for i in range(len(ratio)):
    for j in range(len(ratio[i])):
        text = ax.text(j+1, i+1, round(ratio[i, j], 2),ha="right", va="top", color="w")
ax.set_xticklabels(energybins)
ax.set_yticklabels(ybins)
plt.xlabel("Cluster Energy")
plt.ylabel("5x5 Perimeter Energy")
plt.title("Prompt Photon Fraction")

def myBinnedStat(x,v,bins):
    means,_,_ = stats.binned_statistic(x,v,'mean',bins)
    std,_ ,_= stats.binned_statistic(x,v,'std',bins)
    count,_,_ = stats.binned_statistic(x,v,'count',bins)
    return [ufloat(m,s/(c**(1./2))) for m,s,c in zip(means,std,count)]

然后我可以绘制误差条图,但是我无法在与pcolormesh相同的轴上绘制误差条。我能够用hist2d做到这一点。我不确定为什么会这样。我觉得有一种更干净的方法可以完成整个操作。

这将产生一个图like

1 个答案:

答案 0 :(得分:1)

pcolormesh在x轴上将每个元素绘制为一个单位。也就是说,如果绘制8列,则此数据将在x轴上跨越0-8。但是,您还重新定义了x轴刻度标签,以便将0-10标记为11-21。

对于错误栏,您在11-21处指定了x值,或者看起来如此,这就是绘制数据的位置。但是未标记,因为您更改了ticklabel以使其对应于pcolormesh。

这种差异是您的两个图不对齐的原因。相反,您可以为errorbar使用“默认” x值或为pcolormesh定义x值。例如,使用:

ax.errorbar(range(11), means[0:11], yerr=uncertainties[0:11])