缩放区域嵌入轴

插入轴的示例和显示缩放位置的矩形。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. def get_demo_image():
  4. from matplotlib.cbook import get_sample_data
  5. import numpy as np
  6. f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
  7. z = np.load(f)
  8. # z is a numpy array of 15x15
  9. return z, (-3, 4, -4, 3)
  10. fig, ax = plt.subplots(figsize=[5, 4])
  11. # make data
  12. Z, extent = get_demo_image()
  13. Z2 = np.zeros([150, 150], dtype="d")
  14. ny, nx = Z.shape
  15. Z2[30:30 + ny, 30:30 + nx] = Z
  16. ax.imshow(Z2, extent=extent, interpolation="nearest",
  17. origin="lower")
  18. # inset axes....
  19. axins = ax.inset_axes([0.5, 0.5, 0.47, 0.47])
  20. axins.imshow(Z2, extent=extent, interpolation="nearest",
  21. origin="lower")
  22. # sub region of the original image
  23. x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
  24. axins.set_xlim(x1, x2)
  25. axins.set_ylim(y1, y2)
  26. axins.set_xticklabels('')
  27. axins.set_yticklabels('')
  28. ax.indicate_inset_zoom(axins)
  29. plt.show()

缩放区域嵌入轴示例

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.inset_axes
  3. matplotlib.axes.Axes.indicate_inset_zoom
  4. matplotlib.axes.Axes.imshow

下载这个示例