Skip to content

Instantly share code, notes, and snippets.

@DrSkippy
Created May 1, 2012 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrSkippy/2563891 to your computer and use it in GitHub Desktop.
Save DrSkippy/2563891 to your computer and use it in GitHub Desktop.
Python/Numpy/Scipy/Matplotlib statistical summary and histogram column of numbers
#!/usr/bin/env python
import sys
import numpy
import scipy
import scipy.stats
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-b", "--bins", dest="bins", default=50,
help="Number of bins (default = 50)")
parser.add_option("-c", "--csv", dest="csv", default=False, action="store_true",
help="Set flag for csv output.")
(options, args) = parser.parse_args()
data = []
for i in sys.stdin:
try:
data.append(float(i))
except ValueError:
sys.stderr.write("Input not a number, skipping (%s)\n"%str(i.strip()))
if len(data) <= 2:
sys.stderr.write("Too few data points. Exiting.\n")
sys.exit()
res = ( len(data),
numpy.sum(data),
numpy.mean(data),
numpy.median(data),
numpy.std(data),
numpy.nanmin(data),
numpy.nanmax(data),
scipy.stats.skew(data) )
if options.csv:
print ','.join(map(str,res))
else:
print "n: ....... %d\nsum: ..... %f\nmean: .... %f\nmedian: .. %f\nstd: ..... %f\nmin: ..... %f\nmax: ..... %f\nskew: .... %f"%res
try:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
try:
nbins = int(options.bins)
except ValueError:
nbins = 50
fig = plt.figure()
plt.hist(data, bins=nbins)
fig.savefig("hist.png")
# cdf
fig1 = plt.figure()
[y, x0, bs, xtra] = scipy.stats.cumfreq(data, nbins) # bin values, lowerlimit, binsize, extrapoints
scale = 1.0/y[-1]
y *= scale
x = numpy.linspace(x0, x0 + nbins*bs, nbins)
plt.plot(x, y)
fig1.savefig("cdf.png")
except ImportError:
pass
@rawstar134
Copy link

This is a straightforward trick to generate the histogram. If you like to see the simplest thing, just visit the article where all the things explained very well with code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment