Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e65d48c

Browse files
committed
added api histogram demo which comments on the new bins return value
svn path=/trunk/matplotlib/; revision=5407
1 parent 6372ac8 commit e65d48c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

examples/api/histogram_demo.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Make a histogram of normally distributed random numbers and plot the
3+
analytic PDF over it
4+
"""
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import matplotlib.mlab as mlab
8+
9+
mu, sigma = 100, 15
10+
x = mu + sigma * np.random.randn(10000)
11+
12+
fig = plt.figure()
13+
ax = fig.add_subplot(111)
14+
15+
# the histogram of the data
16+
n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.75)
17+
18+
# hist uses np.histogram under the hood to create 'n' and 'bins'.
19+
# np.histogram returns the bin edges, so there will be 50 probability
20+
# density values in n, 51 bin edges in bins and 50 patches. To get
21+
# everything lined up, we'll compute the bin centers
22+
bincenters = 0.5*(bins[1:]+bins[:-1])
23+
# add a 'best fit' line for the normal PDF
24+
y = mlab.normpdf( bincenters, mu, sigma)
25+
l = ax.plot(bincenters, y, 'r--', linewidth=1)
26+
27+
ax.set_xlabel('Smarts')
28+
ax.set_ylabel('Probability')
29+
#ax.set_title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
30+
ax.set_xlim(40, 160)
31+
ax.set_ylim(0, 0.03)
32+
ax.grid(True)
33+
34+
#fig.savefig('histogram_demo',dpi=72)
35+
plt.show()

0 commit comments

Comments
 (0)