Hi,
I would like to access values in the bins of a matplotlib histogram. The following example script is an attempt to do this. Clearly pdf contains floating point numbers, but I am unable to access them.
Help with this problem would be much appreciated.
Chris
···
--------------------------------------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(20)
#Generate the histogram of the data. Example from Matplotlib documentation
n, bins, patches = plt.hist(x, 50, normed=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
#From Matplotlib documentation.
#normed: If True, the first element of the return tuple will be the counts normalized
#to form a probability density, i.e., n/(len(x)*dbin). In a probability density,
#the integral of the histogram should be 1; you can verify that with a trapezoidal
#integration of the probability density function.
pdf, bins, patches = ax.hist(x, 50, normed=True, facecolor='g', alpha=0.75)
#print pdf shows pdf contains the value in each bin of the normed histogram
print "pdf = ", pdf
print " Integration of PDF = ", np.sum(pdf * np.diff(bins))
#How to access values in pdf? Various tries made but none successful. Example attempt shown
count=0
for line in open(pdf,'r+'):
x=pdf.readline()
z=('%.10f' % float(x))
count=count+1
print "count = ", count
----------------------------------------------------------------------------------------------------