pythonnumpyhistogrambinninghistogram2d

How to align two numpy histograms so that they share the same bins/index, and also transform histogram frequencies to probabilities?


How to convert two datasets X and Y to histograms whose x-axes/index are identical, instead of the x-axis range of variable X being collectively lower or higher than the x-axis range of variable Y (like how the code below generates)? I would like the numpy histogram output values to be ready to plot in a shared histogram-plot afterwards.

import numpy as np
from numpy.random import randn

n = 100  # number of bins

#datasets
X = randn(n)*.1
Y = randn(n)*.2

#empirical distributions
a = np.histogram(X,bins=n)
b = np.histogram(Y,bins=n)

Solution

  • You need not use np.histogram if your goal is just plotting the two (or more) together. Matplotlib can do that.

    import matplotlib.pyplot as plt
    
    plt.hist([X, Y])  # using your X & Y from question
    plt.show()
    

    enter image description here

    If you want the probabilities instead of counts in histogram, add weights:

    wx = np.ones_like(X) / len(X)
    wy = np.ones_like(Y) / len(Y)
    

    You can also get output from plt.hist for some other usage.

    n_plt, bins_plt, patches = plt.hist([X, Y], bins=n-1, weights=[wx,wy])  
    plt.show()
    

    enter image description here

    Note usage of n-1 here instead of n because one extra bin is added by numpy and matplotlib. You may use n depending on your use case.

    However, if you really want the bins for some other purpose, np.historgram gives the bins used in output - which you can use as input in second histogram:

    a,bins_numpy = np.histogram(Y,bins=n-1)
    b,bins2 = np.histogram(X,bins=bins_numpy)
    

    Y's bins used for X here because your Y has wider range than X.

    Reconciliation Checks:

    all(bins_numpy == bins2)
    
    >>>True
    
    
    all(bins_numpy == bins_plt)
    
    >>>True