8000 Support pixel-by-pixel alpha in imshow. · matplotlib/matplotlib@19fa770 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19fa770

Browse files
committed
Support pixel-by-pixel alpha in imshow.
1 parent 6c4bc56 commit 19fa770

File tree

6 files changed

+232
-3
lines changed

6 files changed

+232
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5517,9 +5517,11 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55175517
which can be set by *filterrad*. Additionally, the antigrain image
55185518
resize filter is controlled by the parameter *filternorm*.
55195519
5520-
alpha : scalar, optional
5520+
alpha : [scalar | array_like], optional, default: None
55215521
The alpha blending value, between 0 (transparent) and 1 (opaque).
5522-
This parameter is ignored for RGBA input data.
5522+
If `alpha` is an array, the alpha blending values are applied pixel
5523+
by pixel, and `alpha` must have the same shape as `X`. This
5524+
parameter is ignored for RGBA input data.
55235525
55245526
vmin, vmax : scalar, optional
55255527
When using scalar data and no explicit *norm*, *vmin* and *vmax*

lib/matplotlib/image.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def __init__(self, ax,
257257
self.axes = ax
258258

259259
self._imcache = None
260+
self._array_alpha = None
260261

261262
self.update(kwargs)
262263

@@ -281,7 +282,11 @@ def set_alpha(self, alpha):
281282
----------
282283
alpha : float
283284
"""
284-
martist.Artist.set_alpha(self, alpha)
285+
if np.isscalar(alpha):
286+
martist.Artist.set_alpha(self, alpha)
287+
else:
288+
self._array_alpha = alpha
289+
martist.Artist.set_alpha(self, 1.0)
285290
self._imcache = None
286291

287292
def changed(self):
@@ -487,6 +492,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
487492
# pixel it will be between [0, 1] (such as a rotated image).
488493
out_mask = np.isnan(out_alpha)
489494
out_alpha[out_mask] = 1
495+
# Apply the pixel-by-pixel alpha values if present
496+
if self._array_alpha is not None:
497+
out_alpha *= _resample(self, self._array_alpha, out_shape,
498+
t, resample=True)
490499
# mask and run through the norm
491500
output = self.norm(np.ma.masked_array(A_resampled, out_mask))
492501
else:
Binary file not shown.
Loading

lib/matplotlib/tests/baseline_images/test_image/image_array_alpha.svg

Lines changed: 204 additions & 0 deletions
Loading

lib/matplotlib/tests/test_image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,3 +1083,17 @@ def test_deprecation():
10831083
with pytest.warns(MatplotlibDeprecationWarning):
10841084
# Enough arguments to pass "shape" positionally.
10851085
obj.imshow(data, *[None] * 10)
1086+
1087+
1088+
@image_comparison(baseline_images=['image_array_alpha'], remove_text=True)
1089+
def test_image_array_alpha():
1090+
'''per-pixel alpha channel test'''
1091+
x = np.linspace(0, 1)
1092+
xx, yy = np.meshgrid(x, x)
1093+
1094+
zz = np.exp(- 3 * ((xx - 0.5) ** 2) + (yy -0.7 ** 2))
1095+
alpha = zz / zz.max()
1096+
1097+
fig = plt.figure()
1098+
ax = fig.add_subplot(111)
1099+
ax.imshow(zz, alpha=alpha)

0 commit comments

Comments
 (0)
0