Python Matplotlib:imshow中cmap的作用是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25625952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib : What is the function of cmap in imshow?
提问by Clive
I'm trying to learn opencv using python and came across this code below:
我正在尝试使用 python 学习 opencv 并遇到以下代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv2.imread('opencv_logo.png')
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
source : http://docs.opencv.org/master/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#exercises
来源:http: //docs.opencv.org/master/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#exercises
What does plt.imshow(img1, 'gray') do? I tried searching Google and all I could understand was that the 'gray' argument was a Color map. But my image (pic is there on the site. see link) is not displayed in grayscale. I tried removing the second argument. So the code was like plt.imshow(img1). It executes. The image remains same as before. Then what does the second argument 'gray' do? Can someone explain all this to me? Any help appreciated. Thanks.
plt.imshow(img1, 'gray') 做什么?我尝试在谷歌上搜索,但我能理解的只是“灰色”参数是一张彩色地图。但是我的图像(图片在网站上。见链接)没有以灰度显示。我尝试删除第二个参数。所以代码就像 plt.imshow(img1)。它执行。图像和以前一样。那么第二个参数 'gray' 有什么作用呢?有人可以向我解释这一切吗?任何帮助表示赞赏。谢谢。
PS. I'm totally new to Matplotlib
附注。我对 Matplotlib 完全陌生
采纳答案by unutbu
When img1has shape (M,N,3)or (M,N,4), the values in img1are interpreted as RGB or RGBA values. In this case the cmap is ignored. Per the help(plt.imshow)docstring:
当img1具有 shape(M,N,3)或 时(M,N,4),中的值img1被解释为 RGB 或 RGBA 值。在这种情况下,cmap 将被忽略。每对help(plt.imshow)文档字符串:
cmap :
~matplotlib.colors.Colormap, optional, default: NoneIf None, default to rc
image.cmapvalue.cmapis ignored whenXhas RGB(A) information
cmap :
~matplotlib.colors.Colormap,可选,默认值:无如果没有,默认为 rc
image.cmap值。cmap当X有 RGB(A) 信息时被忽略
However, if imgwere an array of shape (M,N), then the cmap controls the colormap used to display the values.
但是,如果img是 shape 数组(M,N),则 cmap 控制用于显示值的颜色图。
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1 as axes_grid1
np.random.seed(1)
data = np.random.randn(10, 10)
fig = plt.figure()
grid = axes_grid1.AxesGrid(
fig, 111, nrows_ncols=(1, 2), axes_pad = 0.5, cbar_location = "right",
cbar_mode="each", cbar_size="15%", cbar_pad="5%",)
im0 = grid[0].imshow(data, cmap='gray', interpolation='nearest')
grid.cbar_axes[0].colorbar(im0)
im1 = grid[1].imshow(data, cmap='jet', interpolation='nearest')
grid.cbar_axes[1].colorbar(im1)
plt.savefig('/tmp/test.png', bbox_inches='tight', pad_inches=0.0, dpi=200,)



