我有一个用numpy生成的二维直方图:
H, xedges, yedges = np.histogram2d(y, x, weights=mass * (1.0 - pf),
bins=(yrange,xrange))请注意,我目前正在用一个质量函数来衡量垃圾箱(mass是一个与x和y相同尺寸的numpy数组)。回收箱是对数的(通过xrange = np.logspace(minX, maxX, 100)生成)。
然而,我真的想用质量函数来称重这些垃圾箱,但是将它们标准化(即除以)到垃圾箱的面积:例如-每个垃圾箱都有面积xrange[i] * yrange[i]。然而,由于xrange和yrange不具有与mass、x和y相同的维度.我不能简单地将规范化放在np.histogram2d调用中。
如何根据每个日志箱中的区域来标准化回收站计数?
作为参考,这里有一个图(我添加了x和y1D直方图,我也需要用桶的宽度来标准化,但一旦我知道如何对2D进行规范化,就应该类似)。
使用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)))

发布于 2015-10-18 16:49:58
我想你只是想把normed=True传给np.histogram2d
normed:bool,可选 如果是
False,则返回每个bin中的样本数。如果为True,则返回bin密度bin_count / sample_count / bin_area。
如果您想要计算bin区域并手动进行规范化,最简单的方法可能是使用广播业
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))
# Truehttps://stackoverflow.com/questions/33200010
复制相似问题