Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyplot imshow colormap not working

I have the following code:

plt.figure(figsize=(15, 20))
min_v = np.min(net_l0)
max_v = np.max(net_l0)
for i in range(8):
    for j in range(4):
        num = i*4 + j
        plt.subplot(8,4, num+1)
        w_filt = net_l0[num, :3]
        w_filt = w_filt.swapaxes(0, 1).swapaxes(1, 2)
        imgplot = plt.imshow(w_filt, vmin=min_v, vmax=max_v, interpolation='none')
        imgplot.set_cmap('gray')
        plt.colorbar()
plt.show()

For some reason, however, the colormap is not applied to the image only to the colorbar? I tried and adding the cmap keyword to the imshow, but still did not work. Any ideas what I'm doing wrong?

like image 939
Alex Botev Avatar asked Jan 26 '26 07:01

Alex Botev


1 Answers

Make sure the array you are displaying is actually 2-dimensional. If you (for example) load a grayscale image that actually has three channels, then imshow will happily show you the image, but it won't apply the colormap to it. The picture is "already color", after all.

like image 128
Jostikas Avatar answered Jan 29 '26 12:01

Jostikas