Skip to content

Coerce MxNx1 images into MxN images for imshow #15089

Closed

Description

@kmader

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)

Result

TypeError: Invalid shape (3, 3, 1) for image data

if not (self._A.ndim == 2
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
raise TypeError("Invalid shape {} for image data"
.format(self._A.shape))

Expected Result

The same as if I did

plt.imshow(gray_image[:, :, 0])

Activity

  1. added a commit that references this issue on Aug 20, 2019
    5cca2c4
  2. jklymak commented on Aug 20, 2019

    @jklymak
    Member

    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.

  3. kmader commented on Aug 20, 2019

    @kmader
    ContributorAuthor

    @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.

  4. jklymak commented on Aug 20, 2019

    @jklymak
    Member

    OK, but that's not the same as assuming it is grayscale

  5. changed the title [-]Assume images with 1 depth are grayscale[/-] [+]Assume images with depth==1 are the same as 2D arrays[/+] on Aug 20, 2019
  6. changed the title [-]Assume images with depth==1 are the same as 2D arrays[/-] [+]Coerce MxNx1 images into MxN images for imshow[/+] on Aug 20, 2019
  7. tillahoffmann commented on Sep 4, 2019

    @tillahoffmann
    Contributor

    @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.

  8. kmader commented on Sep 4, 2019

    @kmader
    ContributorAuthor

    @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.

  9. added this to the v3.3.0 milestone on Nov 1, 2019
  10. EricBuist commented on May 4, 2021

    @EricBuist

    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.

  11. jklymak commented on May 4, 2021

    @jklymak
    Member

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions