图像演示

在Matplotlib中绘制图像的许多方法。

在Matplotlib中绘制图像最常见的方法是使用 imShow()。下面的示例演示了imShow的许多功能以及您可以创建的许多图像。

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4. import matplotlib.cbook as cbook
  5. from matplotlib.path import Path
  6. from matplotlib.patches import PathPatch

首先,我们将生成一个简单的二元正态分布。

  1. delta = 0.025
  2. x = y = np.arange(-3.0, 3.0, delta)
  3. X, Y = np.meshgrid(x, y)
  4. Z1 = np.exp(-X**2 - Y**2)
  5. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  6. Z = (Z1 - Z2) * 2
  7. fig, ax = plt.subplots()
  8. im = ax.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn,
  9. origin='lower', extent=[-3, 3, -3, 3],
  10. vmax=abs(Z).max(), vmin=-abs(Z).max())
  11. plt.show()

图像演示示例

还可以显示图片的图像。

  1. # A sample image
  2. with cbook.get_sample_data('ada.png') as image_file:
  3. image = plt.imread(image_file)
  4. fig, ax = plt.subplots()
  5. ax.imshow(image)
  6. ax.axis('off') # clear x-axis and y-axis
  7. # And another image
  8. w, h = 512, 512
  9. with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
  10. s = datafile.read()
  11. A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
  12. A /= A.max()
  13. fig, ax = plt.subplots()
  14. extent = (0, 25, 0, 25)
  15. im = ax.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)
  16. markers = [(15.9, 14.5), (16.8, 15)]
  17. x, y = zip(*markers)
  18. ax.plot(x, y, 'o')
  19. ax.set_title('CT density')
  20. plt.show()

图像演示示例2

图像演示示例3

插值图像

也可以在显示图像之前对其进行插值。请注意,因为这可能会影响数据的外观,但它有助于实现您想要的外观。下面我们将显示相同的(小)数组,使用三种不同的插值方法进行插值。

A[i, j]处的像素的中心绘制在 i + 0.5,i + 0.5 处。 如果使用interpolation =’nearest’,则由(i,j) 和 (i + 1, j + 1) 限定的区域将具有相同的颜色。如果使用插值,像素中心的颜色与最近的颜色相同,但其他像素将在相邻像素之间进行插值。

早期版本的matplotlib(<0.63)试图通过设置视图限制来隐藏边缘效果,以便它们不可见。最近在antigrain中的一个误差修复,以及利用此修复的matplotlib._image模块中的一个新实现,不再需要它。为了防止边缘效应,在进行插值时,matplotlib._image模块现在用边缘周围相同的像素填充输入数组。 例如,如果你有一个5x5数组,颜色a-y如下:

  1. a b c d e
  2. f g h i j
  3. k l m n o
  4. p q r s t
  5. u v w x y

_image模块创建填充数组:

  1. a a b c d e e
  2. a a b c d e e
  3. f f g h i j j
  4. k k l m n o o
  5. p p q r s t t
  6. o u v w x y y
  7. o u v w x y y

进行插值/调整大小,然后提取中心区域。这允许你绘制没有边缘效果的阵列的整个范围,例如,使用不同的插值方法将多个不同大小的图像叠加在一起 - 请参阅图层图像。它还意味着性能损失,因为必须创建这个新的临时填充数组。复杂的插值也意味着性能损失,因此如果您需要最大性能或具有非常大的图像,建议插值=“最近”。

  1. A = np.random.rand(5, 5)
  2. fig, axs = plt.subplots(1, 3, figsize=(10, 3))
  3. for ax, interp in zip(axs, ['nearest', 'bilinear', 'bicubic']):
  4. ax.imshow(A, interpolation=interp)
  5. ax.set_title(interp.capitalize())
  6. ax.grid(True)
  7. plt.show()

图像演示示例4

可以使用“原点”参数指定图像应以数组原点 x[0, 0] 绘制在左上角还是右下角。您还可以在matplotLibrary c文件中控制默认设置Image.Source。有关此主题的更多信息,请参阅关于起源和范围的完整指南。

  1. x = np.arange(120).reshape((10, 12))
  2. interp = 'bilinear'
  3. fig, axs = plt.subplots(nrows=2, sharex=True, figsize=(3, 5))
  4. axs[0].set_title('blue should be up')
  5. axs[0].imshow(x, origin='upper', interpolation=interp)
  6. axs[1].set_title('blue should be down')
  7. axs[1].imshow(x, origin='lower', interpolation=interp)
  8. plt.show()

图像演示示例5

最后,我们将使用剪辑路径显示图像。

  1. delta = 0.025
  2. x = y = np.arange(-3.0, 3.0, delta)
  3. X, Y = np.meshgrid(x, y)
  4. Z1 = np.exp(-X**2 - Y**2)
  5. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  6. Z = (Z1 - Z2) * 2
  7. path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
  8. patch = PathPatch(path, facecolor='none')
  9. fig, ax = plt.subplots()
  10. ax.add_patch(patch)
  11. im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray,
  12. origin='lower', extent=[-3, 3, -3, 3],
  13. clip_path=patch, clip_on=True)
  14. im.set_clip_path(patch)
  15. plt.show()

图像演示示例6

参考

下面的示例演示了以下函数和方法的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow
  4. matplotlib.artist.Artist.set_clip_path
  5. matplotlib.patches.PathPatch

下载这个示例