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 ran
Solution 1:
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()
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()
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)
>>>Trueall(bins_numpy == bins_plt)
>>>True
Post a Comment for "How To Align Two Numpy Histograms So That They Share The Same Bins/index, And Also Transform Histogram Frequencies To Probabilities?"