Skip to content

Commit 789dd93

Browse files
SWE-bench ComplexAnthony Shaw
authored andcommitted
Human gold patch for matplotlib__matplotlib-30795
PR: matplotlib#30795 Issue: matplotlib#26092 Base commit: 67ebfc9 Changed lines: 24
1 parent 67ebfc9 commit 789dd93

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

‎lib/matplotlib/image.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
512512
if A.shape[2] == 3: # image has no alpha channel
513513
A = np.dstack([A, np.ones(A.shape[:2])])
514514
elif np.ndim(alpha) > 0: # Array alpha
515-
# user-specified array alpha overrides the existing alpha channel
516-
A = np.dstack([A[..., :3], alpha])
515+
if A.shape[2] == 3: # RGB: use array alpha directly
516+
A = np.dstack([A, alpha])
517+
else: # RGBA: multiply existing alpha by array alpha
518+
A = np.dstack([A[..., :3], A[..., 3] * alpha])
517519
else: # Scalar alpha
518520
if A.shape[2] == 3: # broadcast scalar alpha
519521
A = np.dstack([A, np.full(A.shape[:2], alpha, np.float32)])

‎lib/matplotlib/tests/test_image.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_s
18571857
axs_ref[0][2].imshow(im_rgba, interpolation_stage=intp_stage)
18581858

18591859
# When the image already has an alpha channel, multiply it by the
1860-
# scalar alpha param, or replace it by the array alpha param
1860+
# alpha param (both scalar and array alpha multiply the existing alpha)
18611861
axs_tst[1][0].imshow(im_rgba)
18621862
axs_ref[1][0].imshow(im_rgb, alpha=array_alpha)
18631863
axs_tst[1][1].imshow(im_rgba, interpolation_stage=intp_stage, alpha=scalar_alpha)
@@ -1869,7 +1869,8 @@ def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_s
18691869
new_array_alpha = np.random.rand(ny, nx)
18701870
axs_tst[1][2].imshow(im_rgba, interpolation_stage=intp_stage, alpha=new_array_alpha)
18711871
axs_ref[1][2].imshow(
1872-
np.concatenate( # combine rgb channels with new array alpha
1873-
(im_rgb, new_array_alpha.reshape((ny, nx, 1))), axis=-1
1872+
np.concatenate( # combine rgb channels with multiplied array alpha
1873+
(im_rgb, array_alpha.reshape((ny, nx, 1))
1874+
* new_array_alpha.reshape((ny, nx, 1))), axis=-1
18741875
), interpolation_stage=intp_stage
18751876
)

0 commit comments

Comments
 (0)