This is the script i personally use to make histograms.
import numpy as np
import pylab as P
def binner(LB, UB, step=1):
N = int((UB-LB+1)/float(step) + 1)
return [LB + step * (i - 1/float(2)) for i in xrange(N)]
def f_hist(x, bins=None, param=20, h_type='bar', barwidth=0.8,filename=None):
P.figure()
if bins is None:
if hasattr(param, "__iter__"):
if len(param) == 2:
LB, UB = param
step = 1
elif len(param) == 3:
LB, UB, step = param
else:
raise Exception
bins = binner(LB, UB, step)
n, bins, patches = P.hist(x,bins,histtype=h_type,rwidth=barwidth)
else:
n, bins, patches = P.hist(x,param,histtype=h_type,rwidth=barwidth)
else:
n, bins, patches = P.hist(x, bins, histtype=h_type, rwidth=barwidth)
if filename is None:
P.show()
else:
P.savefig(filename)
print "Histogram saved as {}".format(filename)
return filename
if __name__ == '__main__':
mu, sigma = 200, 25
LB, UB, step = 100, 300, 10
x = mu + sigma*P.randn(10000)
f_hist(x, param = (LB, UB, step))
f_hist(x)
You can see sample case with a normal distribution.