You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Coerce MxNx1 images into MxN images for imshow #15089
Not a bug report and possibly a repeat or bad idea, but using pytorch, keras, theano, tensorflow you often end up with images with shape (x, y, 1). It would be nice, if matplotlib imshow would just assume images of that size are grayscale rather than showing an error message.
import matplotlib.pyplot as plt
import numpy as np
gray_image = np.ones((3, 3, 1))
plt.imshow(gray_image)
Imshow already takes mxn matrix and maps the values into a colormap. If you want that colormap to be grey you can easily do that with the cmap kwarg and can scale with vmin and vmax. I think it’s pretty confusing to say that a mxnx1 array behaves differently.
@jklymak thanks for responding. I think I didn't make it clear. My suggestion would be to have the mxnx1 input interpreted the same way as mxn rather than throwing an error message.
@kmader, we have ended up using plt.imshow(np.squeeze(x)), where x is the output from the network. My hunch is that adding a silent "squeeze" to the interface of imshow could cause confusion in other settings.
@tillahoffmann it would only be a silent squeeze if the 3rd dimension were 1 so it shouldn't change any other behavior, or was there something else you were thinking of?
it just works for MxNx1 images instead of throwing an error. The idea would also be useful for students/beginners who are often confused by such messages and why keras images cannot be shown as they are.
I agree. I ran into this recently and I'm just stuck, cannot render images in grayscale. Probably another function, imshow_grayscale? But would be too specific I imagine. All of this "squeeze" stuff is really confusing and I cannot find ways around this. Documentation is not clear and just enumerating bunches of special cases.
This was closed because we now squeeze mxnx1 images. If you are still having this issue you have an old matplotlib or you should please open a new issue
Activity
Assume grayscale for depth=1 images
jklymak commentedon Aug 20, 2019
Imshow already takes mxn matrix and maps the values into a colormap. If you want that colormap to be grey you can easily do that with the cmap kwarg and can scale with vmin and vmax. I think it’s pretty confusing to say that a mxnx1 array behaves differently.
kmader commentedon Aug 20, 2019
@jklymak thanks for responding. I think I didn't make it clear. My suggestion would be to have the mxnx1 input interpreted the same way as mxn rather than throwing an error message.
jklymak commentedon Aug 20, 2019
OK, but that's not the same as assuming it is grayscale
[-]Assume images with 1 depth are grayscale[/-][+]Assume images with depth==1 are the same as 2D arrays[/+][-]Assume images with depth==1 are the same as 2D arrays[/-][+]Coerce MxNx1 images into MxN images for imshow[/+]tillahoffmann commentedon Sep 4, 2019
@kmader, we have ended up using
plt.imshow(np.squeeze(x)), wherexis the output from the network. My hunch is that adding a silent "squeeze" to the interface ofimshowcould cause confusion in other settings.kmader commentedon Sep 4, 2019
@tillahoffmann it would only be a silent squeeze if the 3rd dimension were 1 so it shouldn't change any other behavior, or was there something else you were thinking of?
it just works for MxNx1 images instead of throwing an error. The idea would also be useful for students/beginners who are often confused by such messages and why keras images cannot be shown as they are.
EricBuist commentedon May 4, 2021
I agree. I ran into this recently and I'm just stuck, cannot render images in grayscale. Probably another function, imshow_grayscale? But would be too specific I imagine. All of this "squeeze" stuff is really confusing and I cannot find ways around this. Documentation is not clear and just enumerating bunches of special cases.
jklymak commentedon May 4, 2021
This was closed because we now squeeze mxnx1 images. If you are still having this issue you have an old matplotlib or you should please open a new issue