Skip to content Skip to sidebar Skip to footer

Which Elements Of List Go Into Which Histogram Bins?

I'm trying to make a scaled scatter plot from a histogram. The scatter plot is fairly straight-forward, make the histogram, find bin centers, scatter plot. nbins=7 # Some example d

Solution 1:

You write

but I want each element of A to only contribute a scaled value to it's bin, that scaled value is stored in B. (i.e. instead of each bin being the count of elements from A for that bin, I want each bin to be the sum of corresponding values from B)

IIUC, this functionality is already supported in numpy.histogram via the weights parameter:

An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count (instead of 1). If normed is True, the weights are normalized, so that the integral of the density over the range remains 1.

So, for your case, it would just be

counts, binEdges=np.histogram(A, bins=nbins, weights=B)

Another point: if your intent is to plot the histogram, note that you can directly use matplotlib.pyplot's utility functions for this (which take weights as well):

from matplotlib import pyplot as plt
plt.hist(A, bins=nbins, weights=B);

enter image description here


Finally, if you're intent on getting the assignments to bins, then that's exactly what numpy.digitize does:

nbins=7
# Some example data
A = np.random.randint(0, 10, 10) 
B = np.random.rand(10)

counts, binEdges=np.histogram(A,bins=nbins)
>>> binEdges, np.digitize(A, binEdges)
array([ 0.        ,  1.28571429,  2.57142857,  3.85714286,  5.14285714,
    6.42857143,  7.71428571,  9.        ])

Post a Comment for "Which Elements Of List Go Into Which Histogram Bins?"