|
| 1 | +Create Inset Axes Without Plotting Twice |
| 2 | +======================================== |
| 3 | + |
| 4 | +:meth:`matplotview.inset_zoom_axes` can be utilized to create inset axes where we |
| 5 | +don't have to plot the parent axes data twice. |
| 6 | + |
| 7 | +.. plot:: |
| 8 | + |
| 9 | + from matplotlib import cbook |
| 10 | + import matplotlib.pyplot as plt |
| 11 | + import numpy as np |
| 12 | + from matplotview import inset_zoom_axes |
| 13 | + |
| 14 | + def get_demo_image(): |
| 15 | + z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) |
| 16 | + # z is a numpy array of 15x15 |
| 17 | + return z, (-3, 4, -4, 3) |
| 18 | + |
| 19 | + fig, ax = plt.subplots() |
| 20 | + |
| 21 | + # Make the data... |
| 22 | + Z, extent = get_demo_image() |
| 23 | + Z2 = np.zeros((150, 150)) |
| 24 | + ny, nx = Z.shape |
| 25 | + Z2[30:30+ny, 30:30+nx] = Z |
| 26 | + |
| 27 | + ax.imshow(Z2, extent=extent, interpolation='nearest', origin="lower") |
| 28 | + |
| 29 | + # Creates an inset axes with automatic view of the parent axes... |
| 30 | + axins = inset_zoom_axes(ax, [0.5, 0.5, 0.47, 0.47]) |
| 31 | + # Set limits to sub region of the original image |
| 32 | + x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 |
| 33 | + axins.set_xlim(x1, x2) |
| 34 | + axins.set_ylim(y1, y2) |
| 35 | + |
| 36 | + # Remove the tick labels from the inset axes |
| 37 | + axins.set_xticklabels([]) |
| 38 | + axins.set_yticklabels([]) |
| 39 | + |
| 40 | + # Draw the indicator or zoom lines. |
| 41 | + ax.indicate_inset_zoom(axins, edgecolor="black") |
| 42 | + |
| 43 | + fig.show() |
| 44 | + |
| 45 | + |
0 commit comments