Quick Tips #2: Try Plotting an All 1s 2D Array in Matplotlib

Siladittya Manna
The Owl
Published in
2 min readOct 21, 2022

Did you try it?

Here’s the code snippet!!

import matplotlib.pyplot as plt
import numpy as np
img1 = np.ones((128,128)) #2D Array of 1splt.imshow(img1, cmap = 'gray')
plt.show()

What did you get?

Is it the same as the image shown below?

Now, let us replace a 32x32 block of the array with zeros and try plotting again using the same code!

img1[21:21+32, 10:10+32] = 0 
# Since, at the time of writing this post, the date is 21/10/2022
plt.imshow(img1)
plt.show()

The image we get from this plot is given below.

Do you see any difference in behaviour?

When using data with values between 0 and 1, the show method understands the data range is [0,1]. But, when the data having all 1s is plotted, the range of the data cannot be evaluated. Hence, the array containing all ones (1 in the range [0,1]) is evaluated as 1 (0x00000001) in the range [0 (0x00000000), 255 (0x11111111)]. Hence, the disparity in the images plotted.

To solve this issue, you can just use the arguments vmin and vmax in the method imshow .

Now, let’s try the code below,

plt.imshow(img1, cmap = 'gray', vmin = 0.0, vmax = 1.0)

plt.show()

The output obtained is given below

Did you see the change?

Another Easy Method

Convert the 2D array into (H, W, C) format

img1 = np.repeat(np.expand_dims(np.ones((128,128)),2),3,2)
plt.imshow(img1)
plt.show()

Clap if you liked this post. Share and Follow for more posts like this.

--

--

Siladittya Manna
The Owl

Senior Research Fellow @ CVPR Unit, Indian Statistical Institute, Kolkata || Research Interest : Computer Vision, SSL, MIA. || https://sadimanna.github.io