Problem plotting white plot using numpy array

Viewed 32
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?

1 Answers

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)
Related