使用不同的权重创建堆叠的二维直方图

Gri*_*iff 5 python numpy histogram

假设我想建立粒子数据的直方图,该直方图在某个 bin 范围 nbin 上进行平滑。现在我有 5 个包含不同质量粒子的数据集(每组 x,y 具有不同的质量)。通常,粒子位置的直方图是一个简单的情况(使用 numpy):

heatmap, xedges, yedges = np.histogram2d(x, y, bins=nbin)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
heatmap = np.flipud(np.rot90(heatmap))
ax.imshow(heatmap, extent=extent)
Run Code Online (Sandbox Code Playgroud)

但是,如果我想添加下一批粒子,它们具有不同的质量,因此密度也会不同。有没有办法通过某个常数对直方图进行加权,以便绘制的热图能够真实地表示密度,而不仅仅是粒子总数的分箱?

我知道“权重”是一个特征,但是否只是设置weights = m_i,其中m_i是每个数据集1-5的粒子质量?

unu*_*tbu 5

该参数需要一个与和weights长度相同的数组。。它不会广播一个常量值,因此即使每次调用的质量相同,您仍然必须使用类似的东西xynp.histogram2dnp.histogram2d

weights=np.ones_like(x)*mass
Run Code Online (Sandbox Code Playgroud)

现在,如果您使用,您可能会遇到的一个问题bin=nbin是 bin 边缘xedges可能yedges会根据您传递给 的x和的值而变化。如果您天真地将热图添加在一起,最终结果将在错误的位置累积粒子密度。ynp.histogram2d

因此,如果您想要np.histogram2d多次调用并将部分热图添加到一起,则必须提前确定您想要箱边缘的位置。

例如:

import numpy as np
import itertools as IT
import matplotlib.pyplot as plt
N = 50
nbin = 10

xs = [np.array([i,i,i+1,i+1]) for i in range(N)]
ys = [np.array([i,i+1,i,i+1]) for i in range(N)]
masses = np.arange(N)

heatmap = 0
xedges = np.linspace(0, N, nbin)
yedges = np.linspace(0, N, nbin)

for x, y, mass in IT.izip(xs, ys, masses):
    hist, xedges, yedges = np.histogram2d(
        x, y, bins=[xedges, yedges], weights=np.ones_like(x)*mass)
    heatmap += hist

extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
heatmap = np.flipud(np.rot90(heatmap))
fig, ax = plt.subplots()
ax.imshow(heatmap, extent=extent, interpolation='nearest')
plt.show()
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述