Fix array alpha to multiply with existing RGBA alpha in imshow by veeceey · Pull Request #31180 · matplotlib/matplotlib · GitHub
Skip to content

Fix array alpha to multiply with existing RGBA alpha in imshow - #31180

Closed
veeceey wants to merge 1 commit into
matplotlib:mainfrom
veeceey:fix/issue-26092-alpha-array-imshow
Closed

veeceey wants to merge 1 commit into
matplotlib:mainfrom
veeceey:fix/issue-26092-alpha-array-imshow

Conversation

@veeceey

@veeceey veeceey commented Feb 20, 2026

Copy link
Copy Markdown

Summary

Closes #26092

  • When passing an array alpha to imshow() with an RGBA image, the alpha values now multiply with the existing alpha channel instead of replacing it
  • This makes array alpha behavior consistent with scalar alpha behavior, which already multiplied with the existing alpha channel
  • For RGB images, array alpha continues to be used directly as the alpha channel (no change in behavior)

The fix is in _ImageBase._make_image() in lib/matplotlib/image.py. Previously, the array alpha code path unconditionally replaced the alpha channel with A = np.dstack([A[..., :3], alpha]). Now it distinguishes between RGB (use alpha directly) and RGBA (multiply A[..., 3] * alpha).

Before (RGBA + array alpha replaces):

elif np.ndim(alpha) > 0:  # Array alpha
    A = np.dstack([A[..., :3], alpha])

After (RGBA + array alpha multiplies):

elif np.ndim(alpha) > 0:  # Array alpha
    if A.shape[2] == 3:  # RGB: use array alpha directly
        A = np.dstack([A, alpha])
    else:  # RGBA: multiply array alpha with existing alpha
        A = np.dstack([A[..., :3], A[..., 3] * alpha])

Test plan

  • Added test_image_array_alpha_rgb - verifies RGB images with array alpha produce same result as manually constructed RGBA
  • Added test_image_array_alpha_rgba - verifies RGBA images with array alpha multiply the existing alpha channel
  • Added RGB + array alpha case to test_imshow_multi_draw parametrization to verify original array is not modified
  • Updated test_interpolation_stage_rgba_respects_alpha_param reference to expect multiplied alpha instead of replaced alpha

🤖 Generated with Claude Code

When passing an array alpha to imshow() with an RGBA image, the alpha
values now multiply with the existing alpha channel rather than
replacing it. This makes array alpha behavior consistent with scalar
alpha behavior, which already multiplied with the existing alpha.

For RGB images, array alpha continues to be used directly as the alpha
channel since there is no pre-existing alpha to blend with.

Closes matplotlib#26092

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

@timhoffm

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: alpha array-type not working with RGB image in imshow()

2 participants