Shan
Shan

Reputation: 19263

How to display images using different color maps in different figures in matplotlib?

I want to display images using different color maps in different figures.

Following code displays the image with two different windows but the same color map

   import scipy.misc
   from pylab import *

   a = scipy.misc.imread('lena.jpg')
   figure(1)
   image = mean(a,axis=2)
   imshow(image)
   #if I call show() here then only one window is displayed
   gray() #change the default colormap to gray
   figure(2)
   imshow(image)
   show()

I am wondering if anyone can please help me.

Thanks a lot.

Upvotes: 6

Views: 40440

Answers (4)

DMTishler
DMTishler

Reputation: 509

If you are attempting to combine 2 figures into 1, i.e. make 1 image. You can use:

import matplotlib.pyplot as plt 

plt.figure()
plt.imshow(im1, cmap=cm.bone)
plt.imshow(im2, cmap=cm.jet,alpha=0.75)
plt.show()

to issue colorbars with relative colormap, call after the imshow():

import matplotlib.pyplot as plt 

plt.figure()

plt.imshow(im1, cmap=cm.bone)
cbar = plt.colorbar(orientation='horizontal')
cbar.set_label('Title (Unit)')

plt.imshow(im2, cmap=cm.jet,alpha=0.75)
cbar = plt.colorbar()
cbar.set_label('Title (Unit)')

plt.show()

Upvotes: 2

Simon
Simon

Reputation: 32933

To do subplots, use the subplot command (!)

To change the colormap, you can use the cmap argument of the imshow function. See the documentation.

figure() # You don't need to specify 1
subplot(121) # 121 is a shortcut for 1 line, 2 columns, item number 1
image = mean(a,axis=2)
imshow(image, cmap='gray')
subplot(122) # 1 line, 2 columns, item number 2
imshow(image, cmap='jet')
show()

Upvotes: 9

Pablo Navarro
Pablo Navarro

Reputation: 8264

You can pass the cmap argument to the imshow function. Look at http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow

Upvotes: 7

schlamar
schlamar

Reputation: 9511

You can use imgplot.set_cmap('gray'). See the huge tutorial.

Upvotes: 6

Related Questions