title: Need help weighting (scaling) each of the bins in a histogram by a different factor
body: <p>I'm trying to make a histogram of the radial distribution of a circular scatterring of particles, and I'm trying to scale the histogram so that the radial distribution is in particles per unit area.</p>
  <blockquote>
  <p><strong>Disclaimer</strong>: If you don't care about the math behind what I'm talking about, just skip over this section:</p>
  <p>I'm splitting the radial distribution in to annuluses of equal width, going out from the center. So, in the center, I will have a circle of some radius, a. The area of this inner most portion will be $\pi a^{2}$.</p>
  <p>Now if we want to know the area of the annulus going from radial distance a to 2a, we do $$ \int_{a}^{2a} 2 \pi r \ dr = 3 \pi a^{2} $$</p>
  <p>Continuing in a similar fashion (going from 2a to 3a, 3a to 4a, etc.) we see that the areas increase as follows: $$ Areas = \pi a^{2}, 3 \pi a^{2}, 5 \pi a^{2}, 7 \pi a^{2}, ... $$</p>
  <p>So, when I weight the histogram for the radial distribution of my scatter, going out from the center, each bin will have to be weighted so that the count of first bin is left alone, the count of the second bin is divided by 3, the count of the third bin is divided by 5, etc, etc.</p>
  </blockquote>
  <p>So: Here's my try at the code:</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  # making random sample of 100000 points between -2.5 and 2.5
  y_vec = 5*np.random.random(100000) - 2.5
  z_vec = 5*np.random.random(100000) - 2.5
  
  # blank canvasses for the y, z, and radial arrays
  y_vec2 = []
  z_vec2 = []
  R_vec =  []
  
  # number of bins I want in the ending histogram
  bns = 40
  
  # cutting out the random samplings that aren't in a circular distribution
  # and making the radial array
  for i in range(0, 100000):
          if np.sqrt((y_vec[i]*y_vec[i] + z_vec[i]*z_vec[i])) &lt;= 2.5:
                  y_vec2.append(y_vec[i])
                  z_vec2.append(z_vec[i])
                  R_vec.append(np.sqrt(y_vec[i]*y_vec[i] + z_vec[i]*z_vec[i]))
  
  # setting up the figures and plots
  fig, ax = plt.subplots()
  fig2, hst = plt.subplots()
  
  # creating a weighting array for the histogram
  wghts = []
  i = 0
  c = 1
  
  # making the weighting array so that each of the bins will be weighted correctly
  # (splitting the radial array up evenly in to groups of the size the bins will be
  # and weighting them appropriately). I assumed the because the documentation says
  # the &quot;weights&quot; array has to be the same size as the &quot;x&quot; initial input, that the 
  # weights act on each point individually...
  while i &lt; bns:
          wghts.extend((1/c)*np.ones(len(R_vec)/bns))
          c = c + 2
          i = i + 1
  
  # Making the plots
  ax.scatter(y_vec2, z_vec2)
  hst.hist(R_vec, bins = bns, weights = wghts)
  
  # plotting
  plt.show()
  </code></pre>
  <p>The scatter plot looks great:
  <img src="https://i.stack.imgur.com/oiLzS.png" alt="Scatter" /></p>
  <p>But, the radial plot suggest that I got the weighting wrong. It should be constant across all annuli, but it is increasing, as though it were not weighted at all:
  <img src="https://i.stack.imgur.com/1Vg8i.png" alt="RadialDist" /></p>
  <p>The erratic look of the Radial Distribution suggests to me that the weighting function in the &quot;hist&quot; operator weights each member of R_vec individually instead of weighting the bins.</p>
  <p>How would I weight the bins by the factors I need to scale them by? Any help?</p>