Search code examples
pythonmatplotlibhistogramdensity-plot

How to calculate upper and lower values of bins of equal density histogram?


I have a range of positive integers ranging from 250-1200, with a normal distribution. I have found the answer to creating bins of equal density (Matplotlib: How to make a histogram with bins of equal area?). What I am actually looking for is to be able to retrieve the upper and lower boundaries of each bin. Is there a library/function that exists for this? or can this information be pulled out from matplotlib?


Solution

  • Let's take a look at the code provided in the question you linked:

    def histedges_equalN(x, nbin):
        npt = len(x)
        return np.interp(np.linspace(0, npt, nbin + 1),
                         np.arange(npt),
                         np.sort(x))
    
    x = np.random.randn(1000)
    n, bins, patches = plt.hist(x, histedges_equalN(x, 10))
    

    bins is actually giving you the edges of each bin as you can read in the docs of hist function:

    enter image description here