开发者

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')
   figu开发者_Python百科re(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.


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


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


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


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜