Fix array alpha to multiply with existing RGBA alpha in imshow#31180
Fix array alpha to multiply with existing RGBA alpha in imshow#31180veeceey wants to merge 1 commit intomatplotlib:mainfrom
Conversation
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]>
|
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. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
|
Thanks for your interest in contributing. However, with #31171 and #30795 we already have open PRs on the topic. Quoting our Contributing guideline
|
Summary
Closes #26092
imshow()with an RGBA image, the alpha values now multiply with the existing alpha channel instead of replacing itThe fix is in
_ImageBase._make_image()inlib/matplotlib/image.py. Previously, the array alpha code path unconditionally replaced the alpha channel withA = np.dstack([A[..., :3], alpha]). Now it distinguishes between RGB (use alpha directly) and RGBA (multiplyA[..., 3] * alpha).Before (RGBA + array alpha replaces):
After (RGBA + array alpha multiplies):
Test plan
test_image_array_alpha_rgb- verifies RGB images with array alpha produce same result as manually constructed RGBAtest_image_array_alpha_rgba- verifies RGBA images with array alpha multiply the existing alpha channeltest_imshow_multi_drawparametrization to verify original array is not modifiedtest_interpolation_stage_rgba_respects_alpha_paramreference to expect multiplied alpha instead of replaced alpha🤖 Generated with Claude Code