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

Appearance settings

Commit 18b1e76

Browse filesBrowse files
committed
respect array alpha with interpolation_stage='rgba'
1 parent 53431a4 commit 18b1e76
Copy full SHA for 18b1e76

File tree

2 files changed

+29
-10
lines changed
Filter options

2 files changed

+29
-10
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+18-10Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,25 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
553553
if A.ndim == 2: # _interpolation_stage == 'rgba'
554554
self.norm.autoscale_None(A)
555555
A = self.to_rgba(A)
556-
alpha = self._get_scalar_alpha()
557-
if A.shape[2] == 3:
558-
# No need to resample alpha or make a full array; NumPy will expand
559-
# this out and cast to uint8 if necessary when it's assigned to the
560-
# alpha channel below.
561-
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
556+
557+
alpha = self.get_alpha()
558+
if alpha is not None and np.ndim(alpha) > 0:
559+
output_alpha = _resample(self, alpha, out_shape, t, resample=True)
560+
output = _resample( # resample rgb channels
561+
# alpha: float, should only be specified when alpha is a scalar
562+
self, _rgb_to_rgba(A[..., :3]), out_shape, t)
562563
else:
563-
output_alpha = _resample( # resample alpha channel
564-
self, A[..., 3], out_shape, t, alpha=alpha)
565-
output = _resample( # resample rgb channels
566-
self, _rgb_to_rgba(A[..., :3]), out_shape, t, alpha=alpha)
564+
alpha = self._get_scalar_alpha()
565+
if A.shape[2] == 3:
566+
# No need to resample alpha or make a full array; NumPy will
567+
# expand this out and cast to uint8 if necessary when it's
568+
# assigned to the alpha channel below.
569+
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
570+
else:
571+
output_alpha = _resample( # resample alpha channel
572+
self, A[..., 3], out_shape, t, alpha=alpha)
573+
output = _resample( # resample rgb channels
574+
self, _rgb_to_rgba(A[..., :3]), out_shape, t, alpha=alpha)
567575
output[..., 3] = output_alpha # recombine rgb and alpha
568576

569577
# output is now either a 2D array of normed (int or float) data

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,3 +1576,14 @@ def test_non_transdata_image_does_not_touch_aspect():
15761576
assert ax.get_aspect() == 1
15771577
ax.imshow(im, transform=ax.transAxes, aspect=2)
15781578
assert ax.get_aspect() == 2
1579+
1580+
1581+
@check_figures_equal()
1582+
def test_interpolation_stage_rgba_does_not_respect_array_alpha(fig_test, fig_ref):
1583+
# GH 28382
1584+
im = np.arange(9).reshape(3, 3)
1585+
alpha = np.linspace(0, 1, 9).reshape(3, 3)
1586+
ax_test = fig_test.subplots()
1587+
ax_test.imshow(im, alpha=alpha, interpolation='none', interpolation_stage='rgba')
1588+
ax_ref = fig_ref.subplots()
1589+
ax_ref.imshow(im, alpha=alpha, interpolation='none')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.