matplotlib.pyplot.imshow( X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)
X : 类数组, shape (n, m) or (n, m, 3) or (n, m, 4)html
在当前的坐标系中显示图像X,X多是数组或者PIL图像,若是是数组:api
MxN – values to be mapped (float or int) MxNx3 – RGB (float or uint8) MxNx4 – RGBA (float or uint8)数组
MxN的每一个数都会经过Normalize和Colormap映射到图像app
若是是整数范围在[0,255],若是是浮点数范围在[0,1]dom
cmap : Colormap, 可选,默认使用rc image.cmap配置的,若是X是三维数组,忽略ui
aspect : [‘auto’ | ‘equal’ | scalar], 可选,默认 rc image.aspect配置值url
若是是‘auto’, changes the 图像长宽比将和坐标系匹配scala
若是是‘equal’, 而且extent为None,坐标系长宽比将匹配图像,若是extent不为None,坐标系的长宽比将匹配extentrest
interpolation : string, 可选,差值方式,默认rc image.interpolation配置值code
能够搜索一下"图像差值"可能更加有助于理解这个参数
‘none’, ‘nearest’, ‘bilinear’, ‘bicubic’, ‘spline16’, ‘spline36’, ‘hanning’, ‘hamming’, ‘hermite’, ‘kaiser’, ‘quadric’, ‘catrom’, ‘gaussian’, ‘bessel’, ‘mitchell’, ‘sinc’, ‘lanczos’之一
norm : Normalize, 可选,颜色映射使用
vmin, vmax : scalar, 可选,若是设置Normalize,忽略
alpha : scalar, 可选,透明度[0,1],若是X使用的是RGBA,忽略
origin : [‘upper’ | ‘lower’], 可选,默认rc image.origin配置值
坐标(0,0)在左下角仍是左上角
extent : scalars (left, right, bottom, top), 可选
数据坐标,图像范围(左下角坐标和右上角坐标)
shape : scalars (columns, rows),可选,图像原生缓冲区
filternorm : scalar, 可选, 默认: 1
给图形大小调整过来使用的参数当filternorm = 1, 将过滤整数值和纠正取整(rounding)错误。不会过滤浮点数。
filterrad : scalar, 可选, 默认: 4.0
为使用半径参数的filter提供,例如:interpolation是‘sinc’, ‘lanczos’ or ‘blackman’中的一个。
image : AxesImage
imshow可能很差理解,由于涉及到不少图形处理相关的知识。咱们先从简单的例子说一下,例如咱们使用MxN数据。为何事2维的呢?由于平面是2维的。MxN中的每个数据都会经过Normalize和Colormap映射出来放到一个位置上,注意MxN不是坐标数据而是像素数据,一个数据对应一个像素位置。
像素数据不能填充指定尺寸的图形怎么办?interpolation就有做用了,选择一个图形插值方式,没有数据的就是用指定的插值方式填充。
# -*- coding:utf-8 -*- import matplotlib.pyplot as plt import numpy as np # extent = (-3,1,-3,1) fig = plt.figure(frameon=False) data = np.add.outer(range(8), range(8)) % 2 print data # plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest',extent=extent) # plt.imshow(data, cmap=plt.cm.gray, interpolation='bilinear',extent=extent) # plt.imshow(data, cmap=plt.cm.gray, interpolation='bicubic',extent=extent) # plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest') plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest',origin="lower") plt.savefig("gray.png") plt.show()
[[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]
输出大概是这个样子滴:
从图像和数据的对比咱们能够比较容易看出,一个数据对应着一个点。坐标周围没有数据的就使用图像插值的方式填充。
再来一个改进版的:
# -*- coding:utf-8 -*- import matplotlib.pyplot as plt import numpy as np dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) extent = np.min(x), np.max(x), np.min(y), np.max(y) # extent = (-3,1,-3,1) fig = plt.figure(frameon=False) dataBase = np.add.outer(range(8), range(8)) % 2 print dataBase plt.imshow(dataBase, cmap=plt.cm.gray, interpolation='nearest',extent=extent) # plt.imshow(dataBase, cmap=plt.cm.gray, interpolation='bilinear',extent=extent) # plt.imshow(dataBase, cmap=plt.cm.gray, interpolation='bicubic',extent=extent) # plt.imshow(dataBase, cmap=plt.cm.gray, interpolation='nearest') dataForeign = np.random.rand(8,8)*3 print dataForeign # plt.imshow(dataForeign, cmap=plt.cm.viridis, alpha=0.9, interpolation='bilinear',extent=extent) plt.imshow(dataForeign, cmap=plt.cm.viridis, alpha=0.9, interpolation='bicubic',extent=extent) plt.savefig("viridis.png") plt.show()
再来一个例子看一看:
# -*- coding:utf-8 -*- import matplotlib.cm as cm import matplotlib.pyplot as plt from matplotlib.patches import Circle, PathPatch from matplotlib.path import Path import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) r = np.random.rand(50) t = np.random.rand(50) * np.pi * 2.0 x = r * np.cos(t) y = r * np.sin(t) fig, ax = plt.subplots(figsize=(6, 6)) # circle = Circle((0, 0), 1, facecolor='none',edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5) circle = Circle((0, 0), 1, facecolor='none',edgecolor='b', linewidth=3) ax.add_patch(circle) ax.axis("off") ax.axis([-2,2,-2,2]) interpolation = ['none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos'] # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='spline36',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='lanczos',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='sinc',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='mitchell',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='bessel',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='gaussian',extent=([-1, 1, -1, 1])) # im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap=cm.winter,interpolation='catrom',extent=([-1, 1, -1, 1])) im = plt.imshow(np.random.random((100, 100)),origin='lower', cmap="CMRmap",interpolation='catrom',extent=([-1, 1, -1, 1])) im.set_clip_path(circle) plt.plot(x, y, 'o', color=(0.9, 0.9, 1.0), alpha=0.8) plt.savefig("circle.png") plt.show()
从上面的例子咱们能够看出imshow在画一下背景和处理蒙版效果仍是很是好的,固然imshow还有其余的功能,好比加载显示图像等。