|
| 1 | +""" |
| 2 | +====================== |
| 3 | +Zoom region inset axes |
| 4 | +====================== |
| 5 | +
|
| 6 | +Example of an inset axes and a rectangle showing where the zoom is located. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +def get_demo_image(): |
| 14 | + from matplotlib.cbook import get_sample_data |
| 15 | + import numpy as np |
| 16 | + f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) |
| 17 | + z = np.load(f) |
| 18 | + # z is a numpy array of 15x15 |
| 19 | + return z, (-3, 4, -4, 3) |
| 20 | + |
| 21 | +fig, ax = plt.subplots(figsize=[5, 4]) |
| 22 | + |
| 23 | +# make data |
| 24 | +Z, extent = get_demo_image() |
| 25 | +Z2 = np.zeros([150, 150], dtype="d") |
| 26 | +ny, nx = Z.shape |
| 27 | +Z2[30:30 + ny, 30:30 + nx] = Z |
| 28 | + |
| 29 | +ax.imshow(Z2, extent=extent, interpolation="nearest", |
| 30 | + origin="lower") |
| 31 | + |
| 32 | +# inset axes.... |
| 33 | +axins = ax.inset_axes([0.5, 0.5, 0.47, 0.47]) |
| 34 | +axins.imshow(Z2, extent=extent, interpolation="nearest", |
| 35 | + origin="lower") |
| 36 | +# sub region of the original image |
| 37 | +x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 |
| 38 | +axins.set_xlim(x1, x2) |
| 39 | +axins.set_ylim(y1, y2) |
| 40 | +axins.set_xticklabels('') |
| 41 | +axins.set_yticklabels('') |
| 42 | + |
| 43 | +ax.zoom_inset_rectangle(axins) |
| 44 | + |
| 45 | +plt.show() |
| 46 | + |
| 47 | +############################################################################# |
| 48 | +# |
| 49 | +# ------------ |
| 50 | +# |
| 51 | +# References |
| 52 | +# """""""""" |
| 53 | +# |
| 54 | +# The use of the following functions and methods is shown in this example: |
| 55 | + |
| 56 | +import matplotlib |
| 57 | +matplotlib.axes.Axes.inset_axes |
| 58 | +matplotlib.axes.Axes.zoom_inset_rectangle |
| 59 | +matplotlib.axes.Axes.imshow |
0 commit comments