|
1 | 1 | import numpy as np
|
2 |
| -import pylab as P |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from matplotlib.mlab import normpdf |
3 | 4 |
|
4 | 5 | #
|
5 | 6 | # The hist() function now has a lot more options
|
|
9 | 10 | # first create a single histogram
|
10 | 11 | #
|
11 | 12 | mu, sigma = 200, 25
|
12 |
| -x = mu + sigma*P.randn(10000) |
| 13 | +x = mu + sigma*np.random.randn(10000) |
13 | 14 |
|
14 | 15 | # the histogram of the data with histtype='step'
|
15 |
| -n, bins, patches = P.hist(x, 50, normed=1, histtype='stepfilled') |
16 |
| -P.setp(patches, 'facecolor', 'g', 'alpha', 0.75) |
| 16 | +n, bins, patches = plt.hist(x, 50, normed=1, histtype='stepfilled') |
| 17 | +plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75) |
17 | 18 |
|
18 | 19 | # add a line showing the expected distribution
|
19 |
| -y = P.normpdf( bins, mu, sigma) |
20 |
| -l = P.plot(bins, y, 'k--', linewidth=1.5) |
| 20 | +y = normpdf( bins, mu, sigma) |
| 21 | +l = plt.plot(bins, y, 'k--', linewidth=1.5) |
21 | 22 |
|
22 | 23 |
|
23 | 24 | #
|
24 | 25 | # create a histogram by providing the bin edges (unequally spaced)
|
25 | 26 | #
|
26 |
| -P.figure() |
| 27 | +plt.figure() |
27 | 28 |
|
28 | 29 | bins = [100,125,150,160,170,180,190,200,210,220,230,240,250,275,300]
|
29 | 30 | # the histogram of the data with histtype='step'
|
30 |
| -n, bins, patches = P.hist(x, bins, normed=1, histtype='bar', rwidth=0.8) |
| 31 | +n, bins, patches = plt.hist(x, bins, normed=1, histtype='bar', rwidth=0.8) |
31 | 32 |
|
32 | 33 | #
|
33 | 34 | # now we create a cumulative histogram of the data
|
34 | 35 | #
|
35 |
| -P.figure() |
| 36 | +plt.figure() |
36 | 37 |
|
37 |
| -n, bins, patches = P.hist(x, 50, normed=1, histtype='step', cumulative=True) |
| 38 | +n, bins, patches = plt.hist(x, 50, normed=1, histtype='step', cumulative=True) |
38 | 39 |
|
39 | 40 | # add a line showing the expected distribution
|
40 |
| -y = P.normpdf( bins, mu, sigma).cumsum() |
| 41 | +y = normpdf( bins, mu, sigma).cumsum() |
41 | 42 | y /= y[-1]
|
42 |
| -l = P.plot(bins, y, 'k--', linewidth=1.5) |
| 43 | +l = plt.plot(bins, y, 'k--', linewidth=1.5) |
43 | 44 |
|
44 | 45 | # create a second data-set with a smaller standard deviation
|
45 | 46 | sigma2 = 15.
|
46 |
| -x = mu + sigma2*P.randn(10000) |
| 47 | +x = mu + sigma2*np.random.randn(10000) |
47 | 48 |
|
48 |
| -n, bins, patches = P.hist(x, bins=bins, normed=1, histtype='step', cumulative=True) |
| 49 | +n, bins, patches = plt.hist(x, bins=bins, normed=1, histtype='step', cumulative=True) |
49 | 50 |
|
50 | 51 | # add a line showing the expected distribution
|
51 |
| -y = P.normpdf( bins, mu, sigma2).cumsum() |
| 52 | +y = normpdf( bins, mu, sigma2).cumsum() |
52 | 53 | y /= y[-1]
|
53 |
| -l = P.plot(bins, y, 'r--', linewidth=1.5) |
| 54 | +l = plt.plot(bins, y, 'r--', linewidth=1.5) |
54 | 55 |
|
55 | 56 | # finally overplot a reverted cumulative histogram
|
56 |
| -n, bins, patches = P.hist(x, bins=bins, normed=1, |
| 57 | +n, bins, patches = plt.hist(x, bins=bins, normed=1, |
57 | 58 | histtype='step', cumulative=-1)
|
58 | 59 |
|
59 | 60 |
|
60 |
| -P.grid(True) |
61 |
| -P.ylim(0, 1.05) |
| 61 | +plt.grid(True) |
| 62 | +plt.ylim(0, 1.05) |
62 | 63 |
|
63 | 64 |
|
64 | 65 | #
|
65 | 66 | # histogram has the ability to plot multiple data in parallel ...
|
66 | 67 | # Note the new color kwarg, used to override the default, which
|
67 | 68 | # uses the line color cycle.
|
68 | 69 | #
|
69 |
| -P.figure() |
| 70 | +plt.figure() |
70 | 71 |
|
71 | 72 | # create a new data-set
|
72 |
| -x = mu + sigma*P.randn(1000,3) |
| 73 | +x = mu + sigma*np.random.randn(1000,3) |
73 | 74 |
|
74 |
| -n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', |
| 75 | +n, bins, patches = plt.hist(x, 10, normed=1, histtype='bar', |
75 | 76 | color=['crimson', 'burlywood', 'chartreuse'],
|
76 | 77 | label=['Crimson', 'Burlywood', 'Chartreuse'])
|
77 |
| -P.legend() |
| 78 | +plt.legend() |
78 | 79 |
|
79 | 80 | #
|
80 | 81 | # ... or we can stack the data
|
81 | 82 | #
|
82 |
| -P.figure() |
| 83 | +plt.figure() |
83 | 84 |
|
84 |
| -n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', stacked=True) |
| 85 | +n, bins, patches = plt.hist(x, 10, normed=1, histtype='bar', stacked=True) |
85 | 86 |
|
86 |
| -P.show() |
| 87 | +plt.show() |
87 | 88 |
|
88 | 89 | #
|
89 | 90 | # we can also stack using the step histtype
|
90 | 91 | #
|
91 | 92 |
|
92 |
| -P.figure() |
| 93 | +plt.figure() |
93 | 94 |
|
94 |
| -n, bins, patches = P.hist(x, 10, histtype='step', stacked=True, fill=True) |
| 95 | +n, bins, patches = plt.hist(x, 10, histtype='step', stacked=True, fill=True) |
95 | 96 |
|
96 |
| -P.show() |
| 97 | +plt.show() |
97 | 98 |
|
98 | 99 | #
|
99 | 100 | # finally: make a multiple-histogram of data-sets with different length
|
100 | 101 | #
|
101 |
| -x0 = mu + sigma*P.randn(10000) |
102 |
| -x1 = mu + sigma*P.randn(7000) |
103 |
| -x2 = mu + sigma*P.randn(3000) |
| 102 | +x0 = mu + sigma*np.random.randn(10000) |
| 103 | +x1 = mu + sigma*np.random.randn(7000) |
| 104 | +x2 = mu + sigma*np.random.randn(3000) |
104 | 105 |
|
105 | 106 | # and exercise the weights option by arbitrarily giving the first half
|
106 | 107 | # of each series only half the weight of the others:
|
|
114 | 115 |
|
115 | 116 |
|
116 | 117 |
|
117 |
| -P.figure() |
| 118 | +plt.figure() |
118 | 119 |
|
119 |
| -n, bins, patches = P.hist( [x0,x1,x2], 10, weights=[w0, w1, w2], histtype='bar') |
| 120 | +n, bins, patches = plt.hist( [x0,x1,x2], 10, weights=[w0, w1, w2], histtype='bar') |
120 | 121 |
|
121 |
| -P.show() |
| 122 | +plt.show() |
0 commit comments