matplotlib之imshow

imshow方法

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)

在当前的坐标系中显示图像X,X可能是数组或者PIL图像,如果是数组:

MxN – values to be mapped (float or int) MxNx3 – RGB (float or uint8) MxNx4 – RGBA (float or uint8)

MxN的每个数都会通过Normalize和Colormap映射到图像

如果是整数范围在[0,255],如果是浮点数范围在[0,1]

cmap : Colormap, 可选,默认使用rc image.cmap配置的,如果X是三维数组,忽略

aspect : [‘auto’ | ‘equal’ | scalar], 可选,默认 rc image.aspect配置值

如果是‘auto’, changes the 图像长宽比将和坐标系匹配

如果是‘equal’, 并且extent为None,坐标系长宽比将匹配图像,如果extent不为None,坐标系的长宽比将匹配extent

interpolation : string, 可选,差值方式,默认rc image.interpolation配置值

可以搜索一下"图像差值"可能更加有助于理解这个参数

扫描二维码关注公众号,回复: 75049 查看本文章

‘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]]

输出大概是这个样子滴:

gray

从图像和数据的对比我们可以比较容易看出,一个数据对应着一个点。坐标周围没有数据的就使用图像插值的方式填充。

再来一个改进版的:

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

viridis

再来一个例子看一看:

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

circle

从上面的例子我们可以看出imshow在画一下背景和处理蒙版效果还是非常好的,当然imshow还有其他的功能,比如加载显示图像等。

参考

matplotlib.pyplot.imshow

Colormaps in Matplotlib

猜你喜欢

转载自my.oschina.net/u/2474629/blog/1800096