|
17 | 17 | import matplotlib.cbook as cbook |
18 | 18 |
|
19 | 19 |
|
20 | | -# Fixing random state for reproducibility |
21 | | -np.random.seed(19680801) |
22 | | - |
23 | 20 | # load up some sample financial data |
24 | 21 | r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data'] |
25 | 22 | .view(np.recarray)) |
|
29 | 26 | pricemin = r.close.min() |
30 | 27 |
|
31 | 28 | ax1.plot(r.date, r.close, lw=2) |
32 | | -ax2.fill_between(r.date, pricemin, r.close, facecolor='blue', alpha=0.5) |
| 29 | +ax2.fill_between(r.date, pricemin, r.close, alpha=0.7) |
33 | 30 |
|
34 | 31 | for ax in ax1, ax2: |
35 | 32 | ax.grid(True) |
|
52 | 49 | # |
53 | 50 | # Our next example computes two populations of random walkers with a |
54 | 51 | # different mean and standard deviation of the normal distributions from |
55 | | -# which the steps are drawn. We use shared regions to plot +/- one |
| 52 | +# which the steps are drawn. We use filled regions to plot +/- one |
56 | 53 | # standard deviation of the mean position of the population. Here the |
57 | 54 | # alpha channel is useful, not just aesthetic. |
58 | 55 |
|
| 56 | +# Fixing random state for reproducibility |
| 57 | +np.random.seed(19680801) |
| 58 | + |
59 | 59 | Nsteps, Nwalkers = 100, 250 |
60 | 60 | t = np.arange(Nsteps) |
61 | 61 |
|
62 | 62 | # an (Nsteps x Nwalkers) array of random walk steps |
63 | | -S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers) |
64 | | -S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers) |
| 63 | +S1 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers) |
| 64 | +S2 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers) |
65 | 65 |
|
66 | 66 | # an (Nsteps x Nwalkers) array of random walker positions |
67 | 67 | X1 = S1.cumsum(axis=0) |
|
77 | 77 |
|
78 | 78 | # plot it! |
79 | 79 | fig, ax = plt.subplots(1) |
80 | | -ax.plot(t, mu1, lw=2, label='mean population 1', color='blue') |
81 | | -ax.plot(t, mu2, lw=2, label='mean population 2', color='yellow') |
82 | | -ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='blue', alpha=0.5) |
83 | | -ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='yellow', alpha=0.5) |
| 80 | +ax.plot(t, mu1, lw=2, label='mean population 1') |
| 81 | +ax.plot(t, mu2, lw=2, label='mean population 2') |
| 82 | +ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='C0', alpha=0.4) |
| 83 | +ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='C1', alpha=0.4) |
84 | 84 | ax.set_title(r'random walkers empirical $\mu$ and $\pm \sigma$ interval') |
85 | 85 | ax.legend(loc='upper left') |
86 | 86 | ax.set_xlabel('num steps') |
|
93 | 93 | # as the x, ymin and ymax arguments, and only fills in the region where |
94 | 94 | # the boolean mask is True. In the example below, we simulate a single |
95 | 95 | # random walker and compute the analytic mean and standard deviation of |
96 | | -# the population positions. The population mean is shown as the black |
97 | | -# dashed line, and the plus/minus one sigma deviation from the mean is |
98 | | -# shown as the yellow filled region. We use the where mask |
99 | | -# ``X > upper_bound`` to find the region where the walker is above the one |
100 | | -# sigma boundary, and shade that region blue. |
| 96 | +# the population positions. The population mean is shown as the dashed |
| 97 | +# line, and the plus/minus one sigma deviation from the mean is shown |
| 98 | +# as the filled region. We use the where mask ``X > upper_bound`` to |
| 99 | +# find the region where the walker is outside the one sigma boundary, |
| 100 | +# and shade that region red. |
| 101 | + |
| 102 | +# Fixing random state for reproducibility |
| 103 | +np.random.seed(1) |
101 | 104 |
|
102 | 105 | Nsteps = 500 |
103 | 106 | t = np.arange(Nsteps) |
|
114 | 117 | upper_bound = mu*t + sigma*np.sqrt(t) |
115 | 118 |
|
116 | 119 | fig, ax = plt.subplots(1) |
117 | | -ax.plot(t, X, lw=2, label='walker position', color='blue') |
118 | | -ax.plot(t, mu*t, lw=1, label='population mean', color='black', ls='--') |
119 | | -ax.fill_between(t, lower_bound, upper_bound, facecolor='yellow', alpha=0.5, |
| 120 | +ax.plot(t, X, lw=2, label='walker position') |
| 121 | +ax.plot(t, mu*t, lw=1, label='population mean', color='C0', ls='--') |
| 122 | +ax.fill_between(t, lower_bound, upper_bound, facecolor='C0', alpha=0.4, |
120 | 123 | label='1 sigma range') |
121 | 124 | ax.legend(loc='upper left') |
122 | 125 |
|
123 | 126 | # here we use the where argument to only fill the region where the |
124 | 127 | # walker is above the population 1 sigma boundary |
125 | | -ax.fill_between(t, upper_bound, X, where=X > upper_bound, facecolor='blue', |
126 | | - alpha=0.5) |
| 128 | +ax.fill_between(t, upper_bound, X, where=X > upper_bound, fc='red', alpha=0.4) |
| 129 | +ax.fill_between(t, lower_bound, X, where=X < lower_bound, fc='red', alpha=0.4) |
127 | 130 | ax.set_xlabel('num steps') |
128 | 131 | ax.set_ylabel('position') |
129 | 132 | ax.grid() |
|
0 commit comments