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

Skip to content

Commit bf7567e

Browse files
committed
no scipy
1 parent 5b448cc commit bf7567e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,32 @@
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

2524
# Generate the space in which the blobs will live
2625
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)
26+
n_bins = 100
27+
xx = np.linspace(xmin, xmax, n_bins)
28+
yy = np.linspace(ymin, ymax, n_bins)
3129

3230
# Generate the blobs. The range of the values is roughly -.0002 to .0002
3331
means_high = [20, 50]
3432
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
33+
var = [150, 200]
34+
35+
def normal_pdf(x, mean, var):
36+
return np.exp(-(x - mean)**2 / (2*var))
37+
38+
gauss_x_high = normal_pdf(xx, means_high[0], var[0])
39+
gauss_y_high = normal_pdf(yy, means_high[1], var[0])
40+
41+
gauss_x_low = normal_pdf(xx, means_low[0], var[1])
42+
gauss_y_low = normal_pdf(yy, means_low[1], var[1])
43+
44+
weights_high = np.array(np.meshgrid(gauss_x_high, gauss_y_high)).prod(0)
45+
weights_low = -1 * np.array(np.meshgrid(gauss_x_low, gauss_y_low)).prod(0)
46+
weights = weights_high + weights_low
3947

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

9098
# Create an alpha channel based on weight values
9199
# Any value whose absolute value is > .0001 will have zero transparency
92-
alphas = Normalize(0, .0001, clip=True)(np.abs(weights))
100+
alphas = Normalize(0, .3, clip=True)(np.abs(weights))
93101
alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
94102

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

108116
# Add contour lines to further highlight different levels.
117+
ax.contour(weights[::-1], levels=[-.1, .1], colors='k', linestyles='-')
118+
ax.set_axis_off()
119+
plt.show()
120+
109121
ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
110122
ax.set_axis_off()
111123
plt.show()

0 commit comments

Comments
 (0)