Blog Archive

Wednesday, November 21, 2012

Histograms in Python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

fig = plt.figure()
ax = fig.add_subplot(111)


binnum=10 #number of bins you want to sort the data into
x=np.array([1,2,2,3,3,3,4,4,4,4,4,5,5,5,5,6,7,8,9,10,10,10,10])
n, bins, patches = ax.hist(x,binnum, normed=1, facecolor='purple', alpha=0.75)


# To get everything lined up, we'll compute the bin centers
bincenters = 0.5*(bins[1:]+bins[:-1])
ax.set_xlabel('x values')
ax.set_ylabel('Probability')
ax.set_title('Histogram')
ax.set_xlim(0,11)
ax.set_ylim(0,1)
ax.grid(True)

plt.show()

No comments:

Post a Comment