Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if A.shape[2] == 3: # image has no alpha channel
A = np.dstack([A, np.ones(A.shape[:2])])
elif np.ndim(alpha) > 0: # Array alpha
# user-specified array alpha overrides the existing alpha channel
A = np.dstack([A[..., :3], alpha])
if A.shape[2] == 3: # image has no alpha channel
A = np.dstack([A, alpha])
else: # blend with existing alpha channel
A = np.dstack([A[..., :3], A[..., 3:] * alpha[..., np.newaxis]])
else: # Scalar alpha
if A.shape[2] == 3: # broadcast scalar alpha
A = np.dstack([A, np.full(A.shape[:2], alpha, np.float32)])
Expand Down
20 changes: 11 additions & 9 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1809,19 +1809,21 @@ def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_s
axs_ref[0][2].imshow(im_rgba, interpolation_stage=intp_stage)

# When the image already has an alpha channel, multiply it by the
# scalar alpha param, or replace it by the array alpha param
# scalar alpha param, or blend it with the array alpha param
axs_tst[1][0].imshow(im_rgba)
axs_ref[1][0].imshow(im_rgb, alpha=array_alpha)
axs_tst[1][1].imshow(im_rgba, interpolation_stage=intp_stage, alpha=scalar_alpha)
axs_ref[1][1].imshow(
np.concatenate( # combine rgb channels with scaled array alpha
(im_rgb, scalar_alpha * array_alpha.reshape((ny, nx, 1))), axis=-1
), interpolation_stage=intp_stage
)
np.concatenate(
(im_rgb,
(scalar_alpha * array_alpha).reshape((ny, nx, 1))),
axis=-1),
interpolation_stage=intp_stage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a style-only change. Please revert.

new_array_alpha = np.random.rand(ny, nx)
axs_tst[1][2].imshow(im_rgba, interpolation_stage=intp_stage, alpha=new_array_alpha)
axs_ref[1][2].imshow(
np.concatenate( # combine rgb channels with new array alpha
(im_rgb, new_array_alpha.reshape((ny, nx, 1))), axis=-1
), interpolation_stage=intp_stage
)
np.concatenate(
(im_rgb,
(array_alpha * new_array_alpha).reshape((ny, nx, 1))),
axis=-1),
interpolation_stage=intp_stage)
Loading