Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按bin区域标准化histogram2d_Python_Arrays_Numpy_Matplotlib - Fatal编程技术网

Python 按bin区域标准化histogram2d

Python 按bin区域标准化histogram2d,python,arrays,numpy,matplotlib,Python,Arrays,Numpy,Matplotlib,我有一个使用numpy生成的2D直方图: H, xedges, yedges = np.histogram2d(y, x, weights=mass * (1.0 - pf), bins=(yrange,xrange)) 请注意,我目前正在称量具有质量函数的箱子(mass是一个numpy数组,其尺寸与x和y相同)。箱子是对数的(通过xrange=np.logspace(minX,maxX,100)生成) 但是,我确实希望通过

我有一个使用numpy生成的2D直方图:

H, xedges, yedges = np.histogram2d(y, x, weights=mass * (1.0 - pf),
                                   bins=(yrange,xrange))
请注意,我目前正在称量具有质量函数的箱子(
mass
是一个numpy数组,其尺寸与
x
y
相同)。箱子是对数的(通过
xrange=np.logspace(minX,maxX,100)
生成)

但是,我确实希望通过质量函数对垃圾箱进行加权,但将其归一化(即除以)为每个垃圾箱的面积:例如,每个垃圾箱的面积
xrange[I]*yrange[I]
。但是,由于
xrange
yrange
质量
的尺寸不同,
x
y
。。。我不能简单地将规范化放在
np.historogram2d
调用中

如何按每个日志存储箱中的区域标准化存储箱计数

作为参考,这里是图(我已经添加了x和y一维直方图,我还需要根据箱子的宽度进行标准化,但一旦我知道如何对二维进行标准化,它应该是类似的)

仅供参考-我使用matplotlib生成主轴直方图:

X,Y=np.meshgrid(xrange,yrange)
H = np.log10(H)
masked_array = np.ma.array(H, mask=np.isnan(H))  # mask out all nan, i.e. log10(0.0)
cax = (ax2dhist.pcolormesh(X,Y,masked_array, cmap=cmap, norm=LogNorm(vmin=1,vmax=8)))

我想你只是想把
normed=True
传递给:

标准化:布尔,可选

如果
False
,则返回每个箱子中的样本数。如果
True
,则返回料仓密度
料仓计数/样本计数/料仓面积


如果您想计算垃圾箱面积并手动执行标准化,最简单的方法可能是使用:


哦,老兄。。。这么简单吗?!我会试着公布结果。谢谢。是的。。。我想就是这样。谢谢你也展示了广播方法。。。在标准化过程中,我还需要使用另一个变量(整个模拟的体积…所以我可以标准化为“每单位体积”。所以这个技巧会派上用场!!
x, y = np.random.rand(2, 1000)
xbin = np.logspace(-1, 0, 101)
ybin = np.logspace(-1, 0, 201)

# raw bin counts
counts, xe, ye = np.histogram2d(x, y, [xbin, ybin])

# size of each bin in x and y dimensions
dx = np.diff(xbin)
dy = np.diff(ybin)

# compute the area of each bin using broadcasting
area = dx[:,  None] * dy

# normalized counts
manual_norm = counts / area / x.shape[0]

# using normed=True
counts_norm, xe, ye = np.histogram2d(x, y, [xbin, ybin], normed=True)

print(np.allclose(manual_norm, counts_norm))
# True