Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit6bbc3c2

Browse files
committed
Support pixel-by-pixel alpha in imshow.
1 parent6c4bc56 commit6bbc3c2

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+
ifnp.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
defchanged(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+
ifself._array_alphaisnotNone:
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.

‎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
withpytest.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+
deftest_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)

[8]ページ先頭

©2009-2025 Movatter.jp