Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60d78ab

Browse filesBrowse files
committed
transparency blend example
1 parent 4033abd commit 60d78ab
Copy full SHA for 60d78ab

File tree

Expand file treeCollapse file tree

1 file changed

+77
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+77
-0
lines changed
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
=============================================
3+
Blend transparency with color with 2-D images
4+
=============================================
5+
6+
Blend transparency with color to highlight parts of data with imshow.
7+
8+
A common use for :func:`matplotlib.pyplot.imshow` is to plot a 2-D statistical
9+
map. In this case, it's common to visualize the statistic of choice (e.g.,
10+
a t-statistic) alongisde another value of interest (e.g., the p-value for that
11+
statistic). One way to do this is to map the p-value onto the transparency of
12+
the image such that data with "significant" values are highlighted.
13+
14+
This example demonstrates how you can achieve this effect using
15+
:class:`matplotlib.colors.Normalize. Note that it is not possible to directly
16+
pass alpha values to :func:`matplotlib.pyplot.imshow`.
17+
18+
First we will generate some data, in this case, we'll create two 2-D "blobs"
19+
in a 2-D grid. One blob will be positive, and the other negative.
20+
"""
21+
# sphinx_gallery_thumbnail_number = 2
22+
import numpy as np
23+
from scipy.stats import multivariate_normal
24+
import matplotlib.pyplot as plt
25+
from matplotlib.colors import Normalize
26+
27+
# Generate the space in which the blobs will live
28+
xmin, xmax, ymin, ymax = (0, 100, 0, 100)
29+
xx = np.linspace(xmin, xmax, 100)
30+
yy = np.linspace(ymin, ymax, 100)
31+
grid = np.array(np.meshgrid(xx, yy))
32+
grid = grid.transpose(2, 1, 0)
33+
34+
# Generate the blobs
35+
means_high = [20, 50]
36+
means_low = [50, 60]
37+
cov = [[500, 0], [0, 800]]
38+
gauss_high = multivariate_normal.pdf(grid, means_high, cov)
39+
gauss_low = -1 * multivariate_normal.pdf(grid, means_low, cov)
40+
weights = gauss_high + gauss_low
41+
42+
# We'll plot these blobs using only ``imshow``.
43+
vmax = np.abs(weights).max() * 1.5
44+
vmin = -vmax
45+
cmap = plt.cm.RdYlBu
46+
fig, ax = plt.subplots()
47+
ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=cmap)
48+
ax.set_axis_off()
49+
50+
################################################################################
51+
# Blending in transparency
52+
# ========================
53+
#
54+
# Below, we'll recreate the same plot, but this time we'll blend in
55+
# transparency with the image so that the extreme values are highlighted.
56+
# We'll also add in contour lines to highlight the image values.
57+
58+
# Create an alpha channel based on weight values
59+
alphas = Normalize(0, .0001, clip=True)(np.abs(weights))
60+
alphas = np.clip(alphas, .4, 1)
61+
62+
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
63+
colors = Normalize(vmin, vmax)(weights)
64+
colors = cmap(colors)
65+
66+
# Now set the alpha channel to the one we created above
67+
colors[..., -1] = alphas
68+
69+
# Create the figure and image
70+
# Note that the absolute values may be slightly different
71+
fig, ax = plt.subplots()
72+
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
73+
74+
# Add contour lines to further highlight different levels.
75+
ax.contour(weights[::-1], levels=[-.0001, .0001], colors='k', linestyles='-')
76+
ax.set_axis_off()
77+
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.