Simple histogram in python,matplotlib (no display, write to png)

Reads from a file called p, uses 10000 bins, filters out values < -10000. Sets a range of -10000 to 3500000, max value of 20.

#!/usr/bin/env python
import numpy as np
import matplotlib as mpl
import matplotlib.mlab as mlab
mpl.use(‘Agg’)
import matplotlib.pyplot as plt

inp = open ("p","r")

x = []
for line in inp.readlines():
  if int(line) > -10000:
    x.append(int(line))

print x
# the histogram of the data
n, bins, patches = plt.hist(x, 10000, normed=0, facecolor=’green’)
print bins
print n

# add a ‘best fit’ line
#y = mlab.normpdf( bins, mu, sigma)
#l = plt.plot(bins, y, ‘r–’, linewidth=1)

plt.xlabel(‘Position’)
plt.ylabel(‘Population’)
plt.title(‘My data’)
#plt.axis([-10000,3500000, 0, 20])
plt.grid(True)

plt.savefig(‘histogram.png’)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>