singularli
singularli

Reputation: 225

Both 0 and 255 give black image for 'gray' color map, why is that?

I'm trying to draw an image with matplotlib.

This code

im_data = np.full((100,100), 0)
axi = plt.imshow(im_data, cmap='gray')

gives me this

enter image description here

however, this code

im_data = np.full((100,100), 255)
axi = plt.imshow(im_data, cmap='gray')

gives me the exact black image as well.

Both 0 and 255 give black image for 'gray' color map, why is that?

I also tried gray and binary colormap and got the same results.

How do I have one of them rendered white image?

Upvotes: 2

Views: 3139

Answers (1)

tdy
tdy

Reputation: 41327

To get your expected black/white output, you need to set vmin and vmax manually. Otherwise pyplot.imshow() infers the min/max from the data. Note that without setting these, any constant value would produce a black image, not just 0 and 255.

vmin, vmax: float, optional

... By default, the colormap covers the complete value range of the supplied data ...

im_data = np.full((100,100), 0)
axi = plt.imshow(im_data, cmap='gray', vmin=0, vmax=255)

0/255

im_data = np.full((100,100), 255)
axi = plt.imshow(im_data, cmap='gray', vmin=0, vmax=255)

255/255

Upvotes: 3

Related Questions