在Python中再现2d直方图

问题描述

我正在使用Python处理非常大的数据集,因此我尝试使用直方图而不是数组(数组太大,无法保存/加载/映射)。我正在抓取一堆文件并从中提取信息,然后我希望获取信息并在之后重新制作直方图。我可以使用一维直方图来做到这一点,如下所示:

counter,bins = np.histogram(nSigmaProtonHisto,bins=1000,range=(-10000,10000))
nSigmaProtonPico[0] += counter
nSigmaProtonPico[1] = bins[:-1]
nSigmaProtonPico是一个2D数组,用于存储bin边缘和直方图值的最终计数。 nSigmaProtonHisto是用于特定事件的一维数组,我遍历了数百万个事件。一旦脚本完成,它将爬过所有事件,并且我将拥有一个带有直方图值和位置的2D数组。我可以简单地绘制图形,就像这样:
plt.plot(nSigmaProtonPico[1],nSigmaProtonPico[0])

enter image description here

当我尝试对2D直方图执行此操作时,它会崩溃。我想念一些东西。这就是我所拥有的:

counter,bins1,bins2 = np.histogram2d(dEdX,pG,range=((0,20),(-5,5)))
dEdXpQPRIME[0] += counter[0]
dEdXpQPRIME[1] += counter[1]
dEdXpQPRIME[2] = bins1[:-1]
dEdXpQPRIME[3] = bins2[:-1]

这使我有所收获,但是我不知道如何绘制它,以便从所有数据中重现我的直方图。我认为这将像x,y和z坐标一样简单,但是只有4个坐标而不是3个坐标。

我想念什么?

解决方法

counter是2D数组。假设您每次histogram2d的调用都具有相同的bin,则将获得相同大小的数组。因此,您可以简单地添加所有counter数组。 考虑:

x1,y1 = np.random.normal(loc=0,scale=1,size=(2,10000))
x2,y2 = np.random.normal(loc=3,10000))

x_bins = np.linspace(-5,5,100)
y_bins = np.linspace(-5,100)

H1,xedges,yedges = np.histogram2d(x1,y1,bins=(x_bins,y_bins))
H2,yedges = np.histogram2d(x2,y2,y_bins))

H1H2均为(99,99)形状(每个维度100个边)。

X,Y = np.meshgrid(xedges,yedges)
H = H1+H2

fig,axs = plt.subplots(1,3,figsize=(9,3))
axs[0].pcolormesh(X,Y,H1)
axs[1].pcolormesh(X,H2)
axs[2].pcolormesh(X,H)

enter image description here