Skip to content

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

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

Fix array alpha to multiply with existing RGBA alpha in imshow#31180
veeceey wants to merge 1 commit intomatplotlib:mainfrom
veeceey:fix/issue-26092-alpha-array-imshow

Conversation

@veeceey
Copy link
Copy Markdown

@veeceey veeceey commented Feb 20, 2026

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 <[email protected]>
@github-actions
Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@timhoffm
Copy link
Copy Markdown
Member

Thanks for your interest in contributing. However, with #31171 and #30795 we already have open PRs on the topic.

Quoting our Contributing guideline

Please check if there is an existing PR for the issue you are addressing. If there is, try to work with the author by submitting reviews of their code or commenting on the PR rather than opening a new PR;

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