import matplotlib import matplotlib.pyplot as plt import pylab import scipy import numpy as np from numpy import histogram from numpy import random from scipy import misc # there is a build-in gaussian normpdf, see below. def gaussian(x, mu, sig): return (1.0/(np.sqrt(2.0*np.pi)*sig) * np.exp(-0.5*(x-mu)*(x-mu)/(sig*sig))) # could plot this too, but caution, wonder carefully what it does when # you ask for the factorial of a non-integer. def poisson(x, mu): return (np.power(mu,x))/scipy.misc.factorial(x) * np.exp(-mu) # use one of these two choices # Your data goes here. # put your data into an array # or read it in from a file data = np.array( [1,2,3,2,1,4,-2,-1,0,-3] ) samplesize = 10 #Alternative, make synthetic random data #samplesize = 10000 #randommean = 1.1 #randomsigma = 1.3 #data = randomsigma * np.random.randn(samplesize) + randommean # specify your preferred bin size lowbound = -6.5 upbound = 6.5 numberofbins = 14 bins = np.linspace(lowbound,upbound,numberofbins) # often for Poisson data, you want the bins to # line up on integers (or two or five or ten integers) # to get the bins to line up on integers, # need to type in 14 = 13+1 instead of 13. # for the range -6.5 to +6.5 . Odd. # I thought I wanted to use pythons built-in histogram class # but it doesn't seem I can actually use it for anything. # myhistogram, bins = np.histogram( data, 9, (-4.5,4.5) ) mymean = np.mean(data) mystdev = np.std(data) print("mean {0} stdev {1} " .format(mymean,mystdev)) plt.figure(facecolor="white") plt.hist(data, bins) plt.title("illustration a Gaussian on data") plt.ylabel("occurances in the data set", fontsize=16) # the fontsize here is gratuitious plt.xlabel("number of counts", fontsize=16) # make a finer binning for to overlay the samplesize normalized Gaussian # caution, what is the meaning of this if you use a poisson expecting integers? x = np.linspace(lowbound,upbound,100) plt.plot(x,samplesize*gaussian(x,mymean,mystdev),'-',linewidth = 3.0, color="black") #plt.plot(x,samplesize*poisson(x,mymean),'-',linewidth = 2.0, color="black") plt.show()