图层图像

使用Alpha混合将图像层叠在彼此之上

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. def func3(x, y):
  4. return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2))
  5. # make these smaller to increase the resolution
  6. dx, dy = 0.05, 0.05
  7. x = np.arange(-3.0, 3.0, dx)
  8. y = np.arange(-3.0, 3.0, dy)
  9. X, Y = np.meshgrid(x, y)
  10. # when layering multiple images, the images need to have the same
  11. # extent. This does not mean they need to have the same shape, but
  12. # they both need to render to the same coordinate system determined by
  13. # xmin, xmax, ymin, ymax. Note if you use different interpolations
  14. # for the images their apparent extent could be different due to
  15. # interpolation edge effects
  16. extent = np.min(x), np.max(x), np.min(y), np.max(y)
  17. fig = plt.figure(frameon=False)
  18. Z1 = np.add.outer(range(8), range(8)) % 2 # chessboard
  19. im1 = plt.imshow(Z1, cmap=plt.cm.gray, interpolation='nearest',
  20. extent=extent)
  21. Z2 = func3(X, Y)
  22. im2 = plt.imshow(Z2, cmap=plt.cm.viridis, alpha=.9, interpolation='bilinear',
  23. extent=extent)
  24. plt.show()

图层图像示例

参考

此示例中显示了以下函数和方法的用法:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow

下载这个示例