在Python中再现二维直方图

2024-10-03 23:18:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python处理一个非常大的数据集,因此我尝试使用直方图而不是数组(数组太大,无法保存/加载/映射)。我在一堆文件上爬行,从中提取信息,然后我想获取信息,然后重新制作直方图。我可以使用1D柱状图进行此操作,如下所示:

counter, bins = np.histogram(nSigmaProtonHisto, bins=1000, range=(-10000, 10000))
nSigmaProtonPico[0] += counter
nSigmaProtonPico[1] = bins[:-1]

nSigmaProtonPico是一个2D数组,用于存储箱边和直方图值的最终计数。nSigmaProtonHisto是一个用于特定事件的一维数组,我循环了数百万个事件。一旦脚本完成,它将爬过所有事件,我将有一个带有直方图值和位置的2D数组。我可以简单地画出它,如下所示:

plt.plot(nSigmaProtonPico[1], nSigmaProtonPico[0])

enter image description here

当我尝试对2D直方图执行此操作时,它会崩溃。我错过了一些东西。以下是我所拥有的:

counter, bins1, bins2 = np.histogram2d(dEdX, pG, bins=1000, 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个坐标

我错过了什么


Tags: 文件数据信息npcounter事件range数组
1条回答
网友
1楼 · 发布于 2024-10-03 23:18:50

counter是一个二维数组。如果每次调用histogram2d时都有相同的bin,那么将得到相同大小的数组。因此,您可以简单地添加所有counter数组。 考虑:

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

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

H1, xedges, yedges = np.histogram2d(x1, y1, bins=(x_bins, y_bins))
H2, xedges, yedges = np.histogram2d(x2, y2, bins=(x_bins, 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, Y, H2)
axs[2].pcolormesh(X, Y, H)

enter image description here

相关问题 更多 >