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

Skip to content

Commit 750d4d8

Browse files
committed
no scipy
1 parent 5b448cc commit 750d4d8

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,34 @@
1818
"""
1919
# sphinx_gallery_thumbnail_number = 3
2020
import numpy as np
21-
from scipy.stats import multivariate_normal
2221
import matplotlib.pyplot as plt
2322
from matplotlib.colors import Normalize
2423

24+
25+
def normal_pdf(x, mean, var):
26+
return np.exp(-(x - mean)**2 / (2*var))
27+
28+
2529
# Generate the space in which the blobs will live
2630
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)
3134

3235
# Generate the blobs. The range of the values is roughly -.0002 to .0002
3336
means_high = [20, 50]
3437
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
3949

4050
# We'll also create a grey background into which the pixels will fade
4151
greys = np.ones(weights.shape + (3,)) * 70
@@ -89,7 +99,7 @@
8999

90100
# Create an alpha channel based on weight values
91101
# 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))
93103
alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
94104

95105
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
@@ -106,6 +116,10 @@
106116
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
107117

108118
# 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+
109123
ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
110124
ax.set_axis_off()
111125
plt.show()

0 commit comments

Comments
 (0)