[SciPy-user] Draw a density line on an histogram

Robert Kern robert.kern at gmail.com
Fri Jan 25 14:01:35 EST 2008


Charles Vejnar wrote:
> Hi,
> 
> I have a series of numbers, and I want to have an idea about their 
> distribution. I start with drawing an histogram with matplotlib.
> 
> But I would like to have a curve (a "smooth histogram") representing the 
> density (which is not a usual function).
> 
> Something with kernel density estimates should be possible. Do you have any 
> idea ?

In [6]: from numpy import *

In [7]: from scipy.stats import gaussian_kde

In [8]: d = random.standard_normal(1000)

In [9]: k = gaussian_kde(d)

In [10]: d.min(), d.max()
Out[10]: (-2.7279408369776075, 3.210698358446658)

In [11]: x = linspace(-3.5, 3.5, 100)

In [12]: y = k(x)


Now plot y versus x.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the SciPy-User mailing list