@TOC
plt.imshow
plt.imshow(X, interpolation=None)
-
X:图像数据
- (M, N):标量数据的图像,灰度图
- (M, N, 3):RGB图像
- (M, N, 4):RGBA图像
注意:其中RGB和RGBA图像为float类型[0, 1],或者int类型[0, 255]
显示图像
Display an image, i.e. data on a 2D regular raster.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import numpy as np import matplotlib.pyplot as plt
np.random.seed(1)
x = np.random.rand(25, 25, 3)
print(x.dtype)
max = np.max(x) x = x*255/max
x = x.astype(int) print(x.dtype) plt.imshow(x) ''' 输出: float64 int32 '''
|
如果最后不显示图像的话,需要再加一句plt.show()
interpolation参数
这里特别讲一下interpolation参数,此参数显示了不同图像之间的插值方式
下面直接给出官方示例:链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import matplotlib.pyplot as plt import numpy as np
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
np.random.seed(19680801)
grid = np.random.rand(4, 4)
fig, axs = plt.subplots(nrows=3, ncols=6, figsize=(9, 6), subplot_kw={'xticks': [], 'yticks': []})
for ax, interp_method in zip(axs.flat, methods): ax.imshow(grid, interpolation=interp_method, cmap='viridis') ax.set_title(str(interp_method))
plt.tight_layout() plt.show()
|