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

Skip to content

Commit 4b7f4a5

Browse files
committed
no scipy
1 parent c46c9ef commit 4b7f4a5

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@
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
23+
from numpy.random import multivariate_normal
2424

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

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

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

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

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

108117
# Add contour lines to further highlight different levels.
118+
ax.contour(weights[::-1], levels=[-.1, .1], colors='k', linestyles='-')
119+
ax.set_axis_off()
120+
plt.show()
121+
109122
ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
110123
ax.set_axis_off()
111124
plt.show()

0 commit comments

Comments
 (0)