Histogram with equal number of points in each bin
<p>I have a sorted vector <code>points</code> with 100 points. I now want to create two histograms: the first histogram should have 10 bins having equal width. The second should also have 10 histograms, but not necessarily of equal width. In the second, I just want the histogram to have the same number of points in each bin. So for example, the first bar might be very short and wide, while the second bar in the histogram might be very tall and narrow. I have code that creates the first histogram using <code>matplotlib</code>, but now I'm not sure how to go about creating the second one.</p>

<pre><code>import matplotlib.pyplot as plt
points = [1,2,3,4,5,6, ..., 99]
n, bins, patches = plt.hist(points, 10)
</code></pre>

<p><strong>Edit:</strong></p>

<p>Trying the solution below, I'm a bit puzzled as to why the heights of all of the bars in my histogram are the same.</p>

<p><a href="http://i.stack.imgur.com/Sr9tk.png" rel="nofollow"><img src="http://i.stack.imgur.com/Sr9tk.png" alt="enter image description here"></a></p>

<p>Here I wrote an example on how you could get the result. My approach uses the data points to get the bins that will be passed to <code>np.histogram</code> to construct the histogram. Hence the need to sort the data using <code>np.argsort(x)</code>. The number of points per bin can be controlled with <code>npoints</code>. As an example, I construct two histograms using this method. One where the weights of all points is the same, so that the height of the histogram is always constant (and equal to <code>npoints</code>). The other where the "weight" of each point is drawn from a uniform random distribution (see <code>mass</code> array). As expected, the boxes of the histogram are not equal anymore. However, the Poisson error per bin is the same.</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="http://i.stack.imgur.com/2EQSs.png" rel="nofollow"><img src="http://i.stack.imgur.com/2EQSs.png" alt="enter image description here"></a></p>

python matplotlib histogram