|
18 | 18 | """
|
19 | 19 | # sphinx_gallery_thumbnail_number = 3
|
20 | 20 | import numpy as np
|
21 |
| -from scipy.stats import multivariate_normal |
22 | 21 | import matplotlib.pyplot as plt
|
23 | 22 | from matplotlib.colors import Normalize
|
24 | 23 |
|
| 24 | + |
| 25 | +def normal_pdf(x, mean, var): |
| 26 | + return np.exp(-(x - mean)**2 / (2*var)) |
| 27 | + |
| 28 | + |
25 | 29 | # Generate the space in which the blobs will live
|
26 | 30 | xmin, xmax, ymin, ymax = (0, 100, 0, 100)
|
27 |
| -xx = np.linspace(xmin, xmax, 100) |
28 |
| -yy = np.linspace(ymin, ymax, 100) |
29 |
| -grid = np.array(np.meshgrid(xx, yy)) |
30 |
| -grid = grid.transpose(2, 1, 0) |
| 31 | +n_bins = 100 |
| 32 | +xx = np.linspace(xmin, xmax, n_bins) |
| 33 | +yy = np.linspace(ymin, ymax, n_bins) |
31 | 34 |
|
32 | 35 | # Generate the blobs. The range of the values is roughly -.0002 to .0002
|
33 | 36 | means_high = [20, 50]
|
34 | 37 | means_low = [50, 60]
|
35 |
| -cov = [[500, 0], [0, 800]] |
36 |
| -gauss_high = multivariate_normal.pdf(grid, means_high, cov) |
37 |
| -gauss_low = -1 * multivariate_normal.pdf(grid, means_low, cov) |
38 |
| -weights = gauss_high + gauss_low |
| 38 | +var = [150, 200] |
| 39 | + |
| 40 | +gauss_x_high = normal_pdf(xx, means_high[0], var[0]) |
| 41 | +gauss_y_high = normal_pdf(yy, means_high[1], var[0]) |
| 42 | + |
| 43 | +gauss_x_low = normal_pdf(xx, means_low[0], var[1]) |
| 44 | +gauss_y_low = normal_pdf(yy, means_low[1], var[1]) |
| 45 | + |
| 46 | +weights_high = np.array(np.meshgrid(gauss_x_high, gauss_y_high)).prod(0) |
| 47 | +weights_low = -1 * np.array(np.meshgrid(gauss_x_low, gauss_y_low)).prod(0) |
| 48 | +weights = weights_high + weights_low |
39 | 49 |
|
40 | 50 | # We'll also create a grey background into which the pixels will fade
|
41 | 51 | greys = np.ones(weights.shape + (3,)) * 70
|
|
89 | 99 |
|
90 | 100 | # Create an alpha channel based on weight values
|
91 | 101 | # Any value whose absolute value is > .0001 will have zero transparency
|
92 |
| -alphas = Normalize(0, .0001, clip=True)(np.abs(weights)) |
| 102 | +alphas = Normalize(0, .3, clip=True)(np.abs(weights)) |
93 | 103 | alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
|
94 | 104 |
|
95 | 105 | # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
|
|
106 | 116 | ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
|
107 | 117 |
|
108 | 118 | # Add contour lines to further highlight different levels.
|
| 119 | +ax.contour(weights[::-1], levels=[-.1, .1], colors='k', linestyles='-') |
| 120 | +ax.set_axis_off() |
| 121 | +plt.show() |
| 122 | + |
109 | 123 | ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
|
110 | 124 | ax.set_axis_off()
|
111 | 125 | plt.show()
|
0 commit comments