Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Fix alpha array not working with RGB images in imshow()#30730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
When using imshow() with RGB images, array-type alpha parameters werebeing ignored in the unsampled code path. This fix ensures that thealpha parameter is passed to to_rgba() so that per-pixel alpha valuesare properly applied to RGB images.The bug was in the _make_image method where the unsampled/no-resamplepath (when not scaling the image) did not pass the alpha parameter toto_rgba(), causing array alpha to be ignored for RGB images while itworked correctly for grayscale images.Fixesmatplotlib#26092
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Pull Request Overview
This PR fixes an issue where array alpha values were not being applied correctly to RGB images in the unsampled code path. The fix passes the alpha parameter to theto_rgba() method call.
Key changes:
- Modified
_make_imageinlib/matplotlib/image.pyto pass alpha parameter in unsampled path - Added test to verify array alpha works with RGB images
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/matplotlib/image.py | Passes alpha parameter toto_rgba() in unsampled code path to fix array alpha handling for RGB images |
| lib/matplotlib/tests/test_image.py | Adds test case to verify array alpha works correctly with RGB images |
💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.
lib/matplotlib/tests/test_image.py Outdated
| # Create an alpha array with varying transparency | ||
| alpha=np.ones_like(arr) | ||
| alpha[:5]=0.3# Top half more transparent |
CopilotAINov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The comment says 'Top half' but the code modifies the first 5 rows out of 10, which is accurate. However, since the array is (10, 10), the comment should be more precise or the description should match the mathematical correctness of the operation.
| alpha[:5]=0.3#Tophalf more transparent | |
| alpha[:5]=0.3#First 5 rows (tophalf) more transparent |
lib/matplotlib/tests/test_image.py Outdated
| """Test that array alpha works with RGB images (issue #26092).""" | ||
| np.random.seed(19680801) | ||
| arr=np.random.random((10,10)) | ||
CopilotAINov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
[nitpick] Remove unnecessary blank line. Python style guides typically limit consecutive blank lines to at most one.
| alpha=np.ones_like(arr) | ||
| alpha[:5]=0.3# Top half more transparent | ||
| fig, (ax1,ax2)=plt.subplots(1,2,figsize=(8,4)) |
CopilotAINov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Variable fig is not used.
| fig,(ax1,ax2)=plt.subplots(1,2,figsize=(8,4)) | |
| (ax1,ax2)=plt.subplots(1,2,figsize=(8,4)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Please revert.
While fig is not used, we have a common convention to still spell it out. One could do_, (ax1, ax2) or_fig, (ax1, ax2). but thefig, (ax1, ax2) pattern is so canonical that there's a value in sticking to it.
The code suggestion is invalid as it would assign the figure to ax1 and the Axes array to ax2.
| plt.imshow(np.zeros((2,2)),alpha=[1,1]) | ||
| @image_comparison(['image_array_alpha_rgb.png'],style='mpl20', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You need to add the baseline image. Seehttps://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test.
rcomer commentedNov 5, 2025
My understanding was that the RGB case had actually been fixed but the issue is kept open because there is a related RGBA problem. Did we break the RGB case again?@Nayil97 what does your test code produce against |
Nayil97 commentedNov 5, 2025
@rcomer@timhoffm Thank you for the feedback! Let me clarify: Testing against |
- Remove extra blank line after arr initialization- Fix comment: 'First 5 rows (top half)' for clarity- Remove unused fig variable from plt.subplots unpackingAddresses@timhoffm and Copilot review feedbackNote: Baseline image will be generated using pytest --mpl-generate-path
Nayil97 commentedNov 5, 2025
@rcomer@timhoffm I've verified the bugDOES still exist on the Bug Verification:I checked self._imcache=self.to_rgba(A,bytes=True,norm=(A.ndim==2)) The Code Style Fixes Applied ✅:
Baseline Image:For the baseline image
Would you prefer I generate this locally and push it, or is there a CI workflow that can generate it during review? Let me know the preferred approach for your project! |
timhoffm commentedNov 5, 2025
We typically generate the baseline images locally. Images are anyway generated by a regular test run, and you can simply copy the generated image to the baseline folder as described herehttps://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test |
Nayil97 commentedNov 6, 2025
Thanks for the feedback@timhoffm! I've added the For the baseline image, I tried to generate it using |
Nayil97 commentedNov 6, 2025
✅Baseline image added! I've successfully added the baseline image
The AppVeyor build should now pass with the baseline image in place. Thanks for your patience@timhoffm and@rcomer! |
Nayil97 commentedNov 6, 2025
The AppVeyor build failed after ~40 minutes, but the logs don't show a specific test failure - they're truncated during test execution. The CircleCI build passed successfully, and all the code review feedback has been addressed. Could we trigger an AppVeyor CI re-run to see if this was a transient timeout/infrastructure issue? The baseline image has been properly added from the previous build's artifacts. |
rcomer commentedNov 7, 2025
The Appveyor log shows 6 test failures starting here: I looked at some of the Github Actions test runners and they are showing more than 6 failures. I also copied the code from your new test and ran it locally against |

Description
This PR fixes issue#26092 where array-type alpha parameters were being ignored when using
imshow()with RGB images, while they worked correctly with grayscale images.Problem
When displaying RGB images with
imshow(), passing an array for thealphaparameter had no effect. The issue was in the unsampled code path of the_make_imagemethod inlib/matplotlib/image.py, where the alpha parameter was not being passed toto_rgba().Solution
Modified line 549 in
lib/matplotlib/image.pyto pass thealphaparameter toto_rgba():This ensures that array alpha values are properly applied to RGB images, consistent with the behavior for grayscale images.
Testing
Added a new test
test_image_array_alpha_rgb()inlib/matplotlib/tests/test_image.pythat verifies:Related Issue
Fixes#26092
Checklist
Note: This is a straightforward bugfix that aligns the RGB image behavior with the existing grayscale image behavior for array alpha parameters.