多重图像

用单一的彩色地图、标准和颜色条制作一组图像。

  1. from matplotlib import colors
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. np.random.seed(19680801)
  5. Nr = 3
  6. Nc = 2
  7. cmap = "cool"
  8. fig, axs = plt.subplots(Nr, Nc)
  9. fig.suptitle('Multiple images')
  10. images = []
  11. for i in range(Nr):
  12. for j in range(Nc):
  13. # Generate data with a range that varies from one plot to the next.
  14. data = ((1 + i + j) / 10) * np.random.rand(10, 20) * 1e-6
  15. images.append(axs[i, j].imshow(data, cmap=cmap))
  16. axs[i, j].label_outer()
  17. # Find the min and max of all colors for use in setting the color scale.
  18. vmin = min(image.get_array().min() for image in images)
  19. vmax = max(image.get_array().max() for image in images)
  20. norm = colors.Normalize(vmin=vmin, vmax=vmax)
  21. for im in images:
  22. im.set_norm(norm)
  23. fig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1)
  24. # Make images respond to changes in the norm of other images (e.g. via the
  25. # "edit axis, curves and images parameters" GUI on Qt), but be careful not to
  26. # recurse infinitely!
  27. def update(changed_image):
  28. for im in images:
  29. if (changed_image.get_cmap() != im.get_cmap()
  30. or changed_image.get_clim() != im.get_clim()):
  31. im.set_cmap(changed_image.get_cmap())
  32. im.set_clim(changed_image.get_clim())
  33. for im in images:
  34. im.callbacksSM.connect('changed', update)
  35. plt.show()

多重图像示例

参考

本例中显示了以下函数、方法和类的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.imshow
  3. matplotlib.pyplot.imshow
  4. matplotlib.figure.Figure.colorbar
  5. matplotlib.pyplot.colorbar
  6. matplotlib.colors.Normalize
  7. matplotlib.cm.ScalarMappable.set_cmap
  8. matplotlib.cm.ScalarMappable.set_norm
  9. matplotlib.cm.ScalarMappable.set_clim
  10. matplotlib.cbook.CallbackRegistry.connect

下载这个示例