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

Skip to content

Commit c46c9ef

Browse files
committed
addressing comments
1 parent 21b70a1 commit c46c9ef

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
"""
2-
=============================================
3-
Blend transparency with color with 2-D images
4-
=============================================
2+
===========================================
3+
Blend transparency with color in 2-D images
4+
===========================================
55
66
Blend transparency with color to highlight parts of data with imshow.
77
88
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`.
9+
map. While ``imshow`` makes it easy to visualize a 2-D matrix as an image,
10+
it doesn't easily let you add transparency to the output. For example, one can
11+
plot a statistic (such as a t-statistic) and color the transparency of
12+
each pixel according to its p-value. This example demonstrates how you can
13+
achieve this effect using :class:`matplotlib.colors.Normalize`. Note that it is
14+
not possible to directly pass alpha values to :func:`matplotlib.pyplot.imshow`.
1715
1816
First we will generate some data, in this case, we'll create two 2-D "blobs"
1917
in a 2-D grid. One blob will be positive, and the other negative.
2018
"""
21-
# sphinx_gallery_thumbnail_number = 2
19+
# sphinx_gallery_thumbnail_number = 3
2220
import numpy as np
2321
from scipy.stats import multivariate_normal
2422
import matplotlib.pyplot as plt
@@ -31,33 +29,68 @@
3129
grid = np.array(np.meshgrid(xx, yy))
3230
grid = grid.transpose(2, 1, 0)
3331

34-
# Generate the blobs
32+
# Generate the blobs. The range of the values is roughly -.0002 to .0002
3533
means_high = [20, 50]
3634
means_low = [50, 60]
3735
cov = [[500, 0], [0, 800]]
3836
gauss_high = multivariate_normal.pdf(grid, means_high, cov)
3937
gauss_low = -1 * multivariate_normal.pdf(grid, means_low, cov)
4038
weights = gauss_high + gauss_low
4139

42-
# We'll plot these blobs using only ``imshow``.
43-
vmax = np.abs(weights).max() * 1.5
40+
# We'll also create a grey background into which the pixels will fade
41+
greys = np.ones(weights.shape + (3,)) * 70
42+
43+
# First we'll plot these blobs using only ``imshow``.
44+
vmax = np.abs(weights).max()
4445
vmin = -vmax
4546
cmap = plt.cm.RdYlBu
47+
4648
fig, ax = plt.subplots()
49+
ax.imshow(greys)
4750
ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=cmap)
4851
ax.set_axis_off()
4952

50-
################################################################################
53+
###############################################################################
5154
# Blending in transparency
5255
# ========================
5356
#
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+
# The simplest way to include transparency when plotting data with
58+
# :func:`matplotlib.pyplot.imshow` is to convert the 2-D data array to a
59+
# 3-D image array of rgba values. This can be done with
60+
# :class:`matplotlib.colors.Normalize`. For example, we'll create a gradient
61+
# moving from left to right below.
62+
63+
# Create an alpha channel of linearly increasing values moving to the right.
64+
alphas = np.ones(weights.shape)
65+
alphas[:, 30:] = np.linspace(1, 0, 70)
66+
67+
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
68+
colors = Normalize(vmin, vmax, clip=True)(weights)
69+
colors = cmap(colors)
70+
71+
# Now set the alpha channel to the one we created above
72+
colors[..., -1] = alphas
73+
74+
# Create the figure and image
75+
# Note that the absolute values may be slightly different
76+
fig, ax = plt.subplots()
77+
ax.imshow(greys)
78+
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
79+
ax.set_axis_off()
80+
81+
###############################################################################
82+
# Using transparency to highlight values with high amplitude
83+
# ==========================================================
84+
#
85+
# Finally, we'll recreate the same plot, but this time we'll use transparency
86+
# to highlight the extreme values in the data. This is often used to highlight
87+
# data points with smaller p-values. We'll also add in contour lines to
88+
# highlight the image values.
5789

5890
# Create an alpha channel based on weight values
91+
# Any value whose absolute value is > .0001 will have zero transparency
5992
alphas = Normalize(0, .0001, clip=True)(np.abs(weights))
60-
alphas = np.clip(alphas, .4, 1)
93+
alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
6194

6295
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
6396
colors = Normalize(vmin, vmax)(weights)
@@ -69,6 +102,7 @@
69102
# Create the figure and image
70103
# Note that the absolute values may be slightly different
71104
fig, ax = plt.subplots()
105+
ax.imshow(greys)
72106
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
73107

74108
# Add contour lines to further highlight different levels.

0 commit comments

Comments
 (0)