<p>在这里,我写了一个关于如何得到结果的例子。我的方法使用数据点来获取将传递给<code>np.histogram</code>来构造直方图的容器。因此需要使用<code>np.argsort(x)</code>对数据进行排序。每个箱子的点数可以用<code>npoints</code>来控制。作为一个例子,我用这个方法构造了两个直方图。其中所有点的权重都相同,因此直方图的高度总是恒定的(等于<code>npoints</code>)。另一个是从均匀随机分布中提取每个点的“权重”(参见<code>mass</code>数组)。正如所料,直方图的方框不再相等。但是,每个箱子的泊松误差是相同的。</p>
<pre><code>x = np.random.rand(1000)
mass = np.random.rand(1000)
npoints = 200
ksort = np.argsort(x)
#Here I get the bins from the data set.
#Note that data need to be sorted
bins=x[ksort[0::npoints]]
bins=np.append(bins,x[ksort[-1]])
fig = plt.figure(1,figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
#Histogram where each data
yhist, xhist = np.histogram(x, bins, weights=None)
ax1.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')
yhist, xhist = np.histogram(x, bins, weights=mass)
ax2.plot(0.5*(xhist[1:]+xhist[:-1]), yhist, linestyle='steps-mid', lw=2, color='k')
ax1.set_xlabel('x', size=15)
ax1.set_ylabel('Number of points per bin', size=15)
ax2.set_xlabel('x', size=15)
ax2.set_ylabel('Mass per bin', size=15)
</code></pre>
<p><a href="https://i.stack.imgur.com/2EQSs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2EQSs.png" alt="enter image description here"/></a></p>