0
import numpy as np import matplotlib as plt a = np.full((256, 256), 255, dtype=np.float32) plt.imshow(a, cmap='gray') 

enter image description here

I want to plot white plot but it is plotting black. I have also tried 0 and 1 in place of 255 but still getting black plot. Can anyone let me know where I am making mistake?

3
  • With such cmap you have to add two other parameters: plt.imshow(a, cmap='gray', vmin=0, vmax=255)
    – Alex
    CommentedSep 10, 2022 at 0:01
  • @Alex can you please explain the reason as well?
    – PScode
    CommentedSep 10, 2022 at 6:26
  • This is just a weird behavior on the part if pyplot when you use this cmap. If you omit this non-default cmap, the issue will disappear even without adding vmin=0, vmax=255, but the image will be greenish in style (more artistic in my opinion).
    – Alex
    CommentedSep 10, 2022 at 12:14

1 Answer 1

0

As mentioned by Alex in the comments, in this case you have to provide vmin=0 and vmax=255. Usually, Matplotlib extracts those values from the image. However, since your array has only one single value, 255, then Matplotlib computes vmin=vmax, which is effectively going to compress the colormap to a single color.

import numpy as np import matplotlib.pyplot as plt a = np.full((256, 256), 255, dtype=int) fig, ax = plt.subplots() ax.imshow(a, cmap='gray', vmin=0, vmax=255) 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.