1+ #!/usr/bin/env python
2+ import pylab as P
3+
4+ #
5+ # The hist() function now has a lot more options
6+ #
7+
8+ #
9+ # first create a single histogram
10+ #
11+ mu , sigma = 200 , 25
12+ x = mu + sigma * P .randn (10000 )
13+
14+ # the histogram of the data with histtype='step'
15+ n , bins , patches = P .hist (x , 50 , normed = 1 , histtype = 'step' )
16+ P .setp (patches , 'facecolor' , 'g' , 'alpha' , 0.75 )
17+
18+ # add a line showing the expected distribution
19+ y = P .normpdf ( bins , mu , sigma )
20+ l = P .plot (bins , y , 'k--' , linewidth = 1.5 )
21+
22+
23+ #
24+ # create a histogram by providing the bin edges (unequally spaced)
25+ #
26+ P .figure ()
27+
28+ bins = [100 ,125 ,150 ,160 ,170 ,180 ,190 ,200 ,210 ,220 ,230 ,240 ,250 ,275 ,300 ]
29+ # the histogram of the data with histtype='step'
30+ n , bins , patches = P .hist (x , bins , normed = 1 , histtype = 'bar' , rwidth = 0.8 )
31+
32+ #
33+ # now we create a cumulative histogram of the data
34+ #
35+ P .figure ()
36+
37+ n , bins , patches = P .hist (x , 50 , normed = 1 , histtype = 'step' , cumulative = True )
38+ P .setp (patches , 'facecolor' , 'b' , 'alpha' , 0.75 )
39+
40+ # add a line showing the expected distribution
41+ y = P .normpdf ( bins , mu , sigma ).cumsum ()
42+ y /= y [- 1 ]
43+ l = P .plot (bins , y , 'k--' , linewidth = 1.5 )
44+
45+ # create a second data-set with a smaller standard deviation
46+ sigma2 = 15.
47+ x = mu + sigma2 * P .randn (10000 )
48+
49+ n , bins , patches = P .hist (x , bins = bins , normed = 1 , histtype = 'step' , cumulative = True )
50+ P .setp (patches , 'facecolor' , 'r' , 'alpha' , 0.5 )
51+
52+ # add a line showing the expected distribution
53+ y = P .normpdf ( bins , mu , sigma2 ).cumsum ()
54+ y /= y [- 1 ]
55+ l = P .plot (bins , y , 'r--' , linewidth = 1.5 )
56+
57+ P .grid (True )
58+ P .ylim (0 , 1.05 )
59+
60+
61+ #
62+ # histogram has the ability to plot multiple data in parallel ...
63+ #
64+ P .figure ()
65+
66+ # create a new data-set
67+ x = mu + sigma * P .randn (1000 ,3 )
68+
69+ n , bins , patches = P .hist (x , 10 , normed = 1 , histtype = 'bar' )
70+
71+ #
72+ # ... or we can stack the data
73+ #
74+ P .figure ()
75+
76+ n , bins , patches = P .hist (x , 10 , normed = 1 , histtype = 'barstacked' )
77+
78+
79+ P .show ()
0 commit comments