Search code examples
pythonmatplotlibhistogramseaborn

Weighted bins in a distribution hist plot


I'm looking for a way to plot a distribution histogram, with the y-axis representing the total number of items for each bin (and not just the count).

Example on the charts below:

  • On the left, there are 55 agencies who sold between 20-30 houses
  • On the right, the agencies having sold between 20-30 houses represent 1100 houses sold

enter image description here

It's not as trivial as it looks because one can't simply multiply each bin's count by the bin's value (maybe in the 20-30 bin, there are 54 agencies who sold 21 are 1 who sold 29).

Questions:

  • What is the name of such a chart (the one on the right)?
  • Is there a way to plot it natively in matplotlib or seaborn?

Solution

  • You want to use the weights kwarg (see numpy docs) which is passed through ax.hist (see).

    Something like

    fig, ax = plt.subplots()
    ax.hist(num_sold, bins, weights=num_sold)