3

I know how to create an histogram in Python, but I would like that it is the probability density distribution.

Let's start with my example. I have an array d, with a size of 500000 elements. With the following code I am building a simple histogram telling me how many elements of my array d are between every bin.

max_val=log10(max(d))
min_val=log10(min(d))

logspace = np.logspace(min_val, max_val, 50)
H=hist(select,bins=logspace,histtype='step')

The problem is that this plot is not what I want. I would like to have the probability distribution function of my array d. Instead of having the number of elements of my array which are within every bins, I would like to have the probability of them being in that bin. I tried with normed=True, but that seems not working since I have bins which are equally logspaced.

Brian
  • 13,996
  • 19
  • 70
  • 94

1 Answers1

2

It's not clear what your hist function is. If you're using NumPy's histogram, try setting density=True. See http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html .

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • Matteo is probably using `hist` from matplotlib, not `histogram` from numpy. – Andrew Jaffe Aug 15 '11 at 08:44
  • 3
    Yes, I am using hist from matplotlib. I tried with histogram from numpy with density=True but I got this error: TypeError: histogram() got an unexpected keyword argument 'density' – Brian Aug 15 '11 at 08:56
  • I got that error as well but it was fixed by updating to numpy 1.6.1. – rgbrgb May 05 '12 at 14:35