0

in astronomy histograms are usually normalized with the bin size (so that the histogram integral is the total number of objects).

I figured out to do an histogram normalized in this way by using numpy, and I was wondering if I can do it directly with matplotlib.

A MWE of the numpy version:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import powerlaw

NBINS = 50

RANGE = [0., 1.]
DELTA = (RANGE[1]-RANGE[0])/float(NBINS)

#--------
# fake data, pretty similar to the real ones
a=1.66
XX = powerlaw.rvs(a, size=1000)
#--------

n, bins = np.histogram(XX, NBINS, normed=False,
        range = RANGE)


ntot = np.sum(n)
n = np.asarray(n, dtype=float)
n/= DELTA

print 'Histogram integral = ', np.sum(n*DELTA), ' total objects = ', ntot
plt.bar(bins[:-1], n, DELTA, log=True)

plt.show()
andrea
  • 151
  • 1
  • 8
  • 1
    possible dublicate https://stackoverflow.com/questions/3866520/plotting-histograms-whose-bar-heights-sum-to-1-in-matplotlib – Glostas Sep 18 '17 at 13:34
  • @Glostas Thanks for finding the duplicate. Next time you may directly use the `close` option below the post to mark as duplicate; this would queue the post in the review, such that people are made aware of it and can vote to close it directly. – ImportanceOfBeingErnest Sep 19 '17 at 10:26
  • @ImportanceOfBeingErnest I have not enough reputation for this... – Glostas Sep 20 '17 at 08:43
  • How can I close it? – andrea Sep 20 '17 at 12:15

0 Answers0