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

Skip to content

Commit 8a5261d

Browse files
committed
Support pixel-by-pixel alpha in imshow.
1 parent 9a1873f commit 8a5261d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5504,9 +5504,11 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55045504
which can be set by *filterrad*. Additionally, the antigrain image
55055505
resize filter is controlled by the parameter *filternorm*.
55065506
5507-
alpha : scalar, optional
5507+
alpha : [scalar | array_like], optional, default: None
55085508
The alpha blending value, between 0 (transparent) and 1 (opaque).
5509-
This parameter is ignored for RGBA input data.
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
5511+
parameter is ignored for RGBA input data.
55105512
55115513
vmin, vmax : scalar, optional
55125514
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:

0 commit comments

Comments
 (0)
0