备注
前往结尾 下载完整示例代码。
imshow 的插值#
此示例展示了 imshow 的不同插值方法之间的差异。
如果 interpolation 为 None,则默认为 rcParams["image.interpolation"] (default: 'auto')。如果插值为 'none',则对于 Agg、ps 和 pdf 后端不进行插值。其他后端将默认为 'auto'。
对于 Agg、ps 和 pdf 后端,当大图像缩小显示时,interpolation='none' 效果良好,而当小图像放大显示时,interpolation='nearest' 效果良好。
关于默认的 interpolation='auto' 选项的讨论,请参见 图像重采样。
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']
# Fixing random state for reproducibility
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()