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

Skip to content

Commit 7a0ae2c

Browse files
committed
Update imshow transparency example.
1 parent 8a5261d commit 7a0ae2c

2 files changed

Lines changed: 18 additions & 31 deletions

File tree

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
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. 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`.
9+
map. The function makes it easy to visualize a 2-D matrix as an image and add
10+
transparency to the output. For example, one can plot a statistic (such as a
11+
t-statistic) and color the transparency of each pixel according to its p-value.
12+
This example demonstrates how you can achieve this effect.
1513
1614
First we will generate some data, in this case, we'll create two 2-D "blobs"
1715
in a 2-D grid. One blob will be positive, and the other negative.
@@ -50,42 +48,38 @@ def normal_pdf(x, mean, var):
5048
# We'll also create a grey background into which the pixels will fade
5149
greys = np.full((*weights.shape, 3), 70, dtype=np.uint8)
5250

53-
# First we'll plot these blobs using only ``imshow``.
51+
# First we'll plot these blobs using ``imshow`` without transparency.
5452
vmax = np.abs(weights).max()
55-
vmin = -vmax
56-
cmap = plt.cm.RdYlBu
53+
imshow_kwargs = {
54+
'vmax': vmax,
55+
'vmin': -vmax,
56+
'cmap': 'RdYlBu',
57+
'extent': (xmin, xmax, ymin, ymax),
58+
}
5759

5860
fig, ax = plt.subplots()
5961
ax.imshow(greys)
60-
ax.imshow(weights, extent=(xmin, xmax, ymin, ymax), cmap=cmap)
62+
ax.imshow(weights, **imshow_kwargs)
6163
ax.set_axis_off()
6264

6365
###############################################################################
6466
# Blending in transparency
6567
# ========================
6668
#
6769
# The simplest way to include transparency when plotting data with
68-
# :func:`matplotlib.pyplot.imshow` is to convert the 2-D data array to a
69-
# 3-D image array of rgba values. This can be done with
70-
# :class:`matplotlib.colors.Normalize`. For example, we'll create a gradient
70+
# :func:`matplotlib.pyplot.imshow` is to pass an array matching the shape of
71+
# the data to the ``alpha`` argument. For example, we'll create a gradient
7172
# moving from left to right below.
7273

7374
# Create an alpha channel of linearly increasing values moving to the right.
7475
alphas = np.ones(weights.shape)
7576
alphas[:, 30:] = np.linspace(1, 0, 70)
7677

77-
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
78-
colors = Normalize(vmin, vmax, clip=True)(weights)
79-
colors = cmap(colors)
80-
81-
# Now set the alpha channel to the one we created above
82-
colors[..., -1] = alphas
83-
8478
# Create the figure and image
8579
# Note that the absolute values may be slightly different
8680
fig, ax = plt.subplots()
8781
ax.imshow(greys)
88-
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
82+
ax.imshow(weights, alpha=alphas, **imshow_kwargs)
8983
ax.set_axis_off()
9084

9185
###############################################################################
@@ -102,18 +96,11 @@ def normal_pdf(x, mean, var):
10296
alphas = Normalize(0, .3, clip=True)(np.abs(weights))
10397
alphas = np.clip(alphas, .4, 1) # alpha value clipped at the bottom at .4
10498

105-
# Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow
106-
colors = Normalize(vmin, vmax)(weights)
107-
colors = cmap(colors)
108-
109-
# Now set the alpha channel to the one we created above
110-
colors[..., -1] = alphas
111-
11299
# Create the figure and image
113100
# Note that the absolute values may be slightly different
114101
fig, ax = plt.subplots()
115102
ax.imshow(greys)
116-
ax.imshow(colors, extent=(xmin, xmax, ymin, ymax))
103+
ax.imshow(weights, alpha=alphas, **imshow_kwargs)
117104

118105
# Add contour lines to further highlight different levels.
119106
ax.contour(weights[::-1], levels=[-.1, .1], colors='k', linestyles='-')

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5506,8 +5506,8 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55065506
55075507
alpha : [scalar | array_like], optional, default: None
55085508
The alpha blending value, between 0 (transparent) and 1 (opaque).
5509-
If `alpha` is an array, the alpha blending values are applied pixel
5510-
by pixel, and `alpha` must have the same shape as `X`. This
5509+
If *alpha* is an array, the alpha blending values are applied pixel
5510+
by pixel, and *alpha* must have the same shape as *X*. This
55115511
parameter is ignored for RGBA input data.
55125512
55135513
vmin, vmax : scalar, optional

0 commit comments

Comments
 (0)