Tuesday, April 13, 2010

histograms with chaco

i searched in vain for a quick and easy histogram plotter in chaco from enthought. found this snippet for using numpy.histogram, which i should be able to adapt to chaco if i need to later.

Histograms

The NumPy's function histogram applied to an array returns a pair of vectors: the histogram of the array and the vector of bins. Beware:matplotlib has also a function to build histograms (called hist, as in matlab) different from that of NumPy. The main difference is thatpylab.hist plots the histogram automatically, while numpy.histogram only generates the data.

import numpy import pylab # Build a vector of 10000 normal deviates with variance 0.5^2 and mean 2 mu, sigma = 2, 0.5 v = numpy.random.normal(mu,sigma,10000) # Plot a normalized histogram with 50 bins pylab.hist(v, bins=50, normed=1)       # matplotlib version (plot) pylab.show() # Compute the histogram with numpy and then plot it (n, bins) = numpy.histogram(v, bins=50, normed=1)  # NumPy version (no plot) pylab.plot(.5*(bins[1:]+bins[:-1]), n) pylab.show()

No comments: