-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: imshow
allows RGB(A) images with np.nan
values to pass
#27301
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
colormaps aren't used for 3 or 4 channel imshow()'s, so the "bad" is never
applicable there. With the 4 channel one, having an alpha of zero just
reveals the color underneath.
…On Thu, Nov 9, 2023 at 4:37 PM Emilio Graff ***@***.***> wrote:
Bug summary
Using imshow to visualize a 2x2x1 array with nans scattered throughout
works as expected. With the default "bad" color of black with alpha = 0,
you'll see an axis' facecolor where the nans are.
With a 2x2x3 array, the nans are always black.
With a 2x2x4 array, and the alpha manually set to 0 where the nans are,
you get the correct behavior (i.e. you can see the axis where the nans are.
Code for reproduction
import numpyfrom matplotlib import pyplot
img = numpy.ones((2, 2)) # single channel 2x2img_nan = img.copy()img_nan[0, 0] = float('nan')
img3 = numpy.ones((2, 2, 3)) # RGB 2x2img3_nan = img3.copy()img3_nan[0, 0, :] = float('nan')
img4 = numpy.ones((2, 2, 4)) # RGBA 2x2img4_nan = img4.copy()img4_nan[0, 0, :] = float('nan')img4_nan[0, 0, 3] = 0 # alpha
def imshow(a):
pyplot.figure()
im = pyplot.imshow(a)
# im.get_cmap().set_bad(color='y', alpha=1) # only works for single channel images
im.get_cmap().set_bad(alpha=0)
pyplot.gca().set_facecolor('y')
imshow(img_nan) # nan shows up as 'y'imshow(img3_nan) # nan shows up as blackimshow(img4_nan) # nan shows up as 'y'
pyplot.show()
Actual outcome
[image: image]
<https://user-images.githubusercontent.com/26465923/281868384-bc067e95-da1a-4ddf-b55b-929b8a18054b.png>
[image: image]
<https://user-images.githubusercontent.com/26465923/281868405-7afe79b4-64cc-4b41-a909-d2d6fe966af0.png>
[image: image]
<https://user-images.githubusercontent.com/26465923/281868433-ef71c33e-991d-4fd5-95a2-80ce3a66584c.png>
Expected outcome
Figures 2 and 3 above should look identical.
Additional information
I traced through imshow and cannot see any difference between the single-
and 3-channel image; the mask is there and it's correct in both cases. So I
wonder if this is a backend issue?
Operating system
Windows
Matplotlib Version
3.7.3
Matplotlib Backend
QtAgg
Python version
3.8.10
Jupyter version
*No response*
Installation
pip
—
Reply to this email directly, view it on GitHub
<#27301>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACHF6FMWP2UDJGCCPC3SILYDVEJRAVCNFSM6AAAAAA7FHKK6KVHI2DSMVQWIX3LMV43ASLTON2WKOZRHE4DMNBUGA4DGOA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
There's an additional thing here. Moving the mouse pointer over the image:
|
OK fine, colormaps are ignored for 3- and 4-channel images. I guess I'm arguing that masking out |
For float RGB(A) images I am not sure what I would expect My inclination is that the second two cases should be raising on invalid input (as they would if you passed |
and the reason the second RGB(A) case looks like it works is that the alpha channel is 0 so it does not matter what the RGB channels do. |
set_bad
doesn't work with imshow
and 3-channel imagesimshow
allows RGB(A) images with np.nan
values to pass
@tacaswell, if I understand correctly, you're now going to raise an exception if an RGB image has This seems quite harsh, since single-channel images are allowed to have My RGBA example is a workaround; in our application, I went that route because I want 1- and 3- channel images to behave the same with regards to |
I think I would expect points with nans to be treated the same as masked points. We have explicit handling that sets the alpha to zero if any of the channels are masked at the given point: matplotlib/lib/matplotlib/cm.py Lines 501 to 504 in c0e9ebc
|
Bug summary
Using
imshow
to visualize a 2x2x1 array with nans scattered throughout works as expected. With the default "bad" color of black with alpha = 0, you'll see an axis'facecolor
where thenan
s are.With a 2x2x3 array, the
nan
s are always black.With a 2x2x4 array, and the alpha manually set to 0 where the
nan
s are, you get the correct behavior (i.e. you can see the axis where the nans are).Code for reproduction
Actual outcome
Expected outcome
Figures 2 and 3 above should look identical.
Additional information
I traced through
imshow
and cannot see any difference between the single- and 3-channel image; the mask is there and it's correct in both cases. So I wonder if this is a backend issue?Operating system
Windows
Matplotlib Version
3.7.3
Matplotlib Backend
QtAgg
Python version
3.8.10
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: