Created
September 3, 2012 08:15
-
-
Save astanin/3607836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import scipy as sp | |
import matplotlib.pyplot as plt | |
SAMPLES=10000 | |
BINS=50 | |
xs = sp.linspace(-5,5,100) | |
fs = sp.randn(SAMPLES) # normally distributed samples | |
pdf = 1.0/sp.sqrt(2*sp.pi)*sp.exp(-0.5*xs**2) | |
def plothist(heights, bins, name='', color='skyblue'): | |
"Plot histogram and PDF from the output of sp.histogram." | |
fig = plt.figure() | |
p = fig.add_subplot(111) | |
p.bar(bins[:-1], heights, width=bins[1]-bins[0], color=color, label=name) | |
p.plot(xs, pdf, 'r-', label='pdf') | |
p.legend() | |
p.set_xlabel("x") | |
p.set_ylabel("d") | |
p.set_ylim((0,0.5)) | |
return fig | |
# method 0: a histogram normalized by SciPy | |
hs0, bins0 = sp.histogram(fs, bins=BINS, density=True) | |
fig0 = plothist(hs0, bins0, 'scipy normalization') | |
# method 1: divide by sum (@virens) | |
hs, bins = sp.histogram(fs, bins=BINS) | |
hs1 = hs/float(hs.sum()) | |
fig1 = plothist(hs1, bins, 'div by sum (virens@)') | |
# method 1b: divide by sum and bin size (fixed) | |
hs1b = hs/(float(hs.sum())*(bins[1]-bins[0])) | |
fig1b = plothist(hs1b, bins, 'div by sum AND BIN SIZE') | |
# method 2: divide by area | |
hs2 = hs/sp.trapz(hs, x=bins[:-1]) | |
fig2 = plothist(hs2, bins, 'div by area') | |
fig0.savefig("normhist-scipy.png") | |
fig1.savefig("normhist-divbysum-bad.png") | |
fig1b.savefig("normhist-divbysum-fixed.png") | |
fig2.savefig("normhist-trapz.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment