|
8 | 8 |
|
9 | 9 | from mpl_toolkits.axes_grid1 import make_axes_locatable
|
10 | 10 | from mpl_toolkits.axes_grid1 import AxesGrid
|
| 11 | +from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset |
| 12 | +from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar |
11 | 13 |
|
12 | 14 | from matplotlib.colors import LogNorm
|
13 | 15 |
|
@@ -72,6 +74,55 @@ def test_axesgrid_colorbar_log_smoketest():
|
72 | 74 | grid.cbar_axes[0].colorbar(im)
|
73 | 75 |
|
74 | 76 |
|
| 77 | +@image_comparison( |
| 78 | + baseline_images=['inset_locator'], style='default', extensions=['png'], |
| 79 | + remove_text=True) |
| 80 | +def test_inset_locator(): |
| 81 | + def get_demo_image(): |
| 82 | + from matplotlib.cbook import get_sample_data |
| 83 | + import numpy as np |
| 84 | + f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) |
| 85 | + z = np.load(f) |
| 86 | + # z is a numpy array of 15x15 |
| 87 | + return z, (-3, 4, -4, 3) |
| 88 | + |
| 89 | + fig, ax = plt.subplots(figsize=[5, 4]) |
| 90 | + |
| 91 | + # prepare the demo image |
| 92 | + Z, extent = get_demo_image() |
| 93 | + Z2 = np.zeros([150, 150], dtype="d") |
| 94 | + ny, nx = Z.shape |
| 95 | + Z2[30:30 + ny, 30:30 + nx] = Z |
| 96 | + |
| 97 | + # extent = [-3, 4, -4, 3] |
| 98 | + ax.imshow(Z2, extent=extent, interpolation="nearest", |
| 99 | + origin="lower") |
| 100 | + |
| 101 | + axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6 |
| 102 | + axins.imshow(Z2, extent=extent, interpolation="nearest", |
| 103 | + origin="lower") |
| 104 | + |
| 105 | + # sub region of the original image |
| 106 | + x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 |
| 107 | + axins.set_xlim(x1, x2) |
| 108 | + axins.set_ylim(y1, y2) |
| 109 | + |
| 110 | + plt.xticks(visible=False) |
| 111 | + plt.yticks(visible=False) |
| 112 | + |
| 113 | + # draw a bbox of the region of the inset axes in the parent axes and |
| 114 | + # connecting lines between the bbox and the inset axes area |
| 115 | + mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") |
| 116 | + |
| 117 | + asb = AnchoredSizeBar(ax.transData, |
| 118 | + 0.5, |
| 119 | + '0.5', |
| 120 | + loc=8, |
| 121 | + pad=0.1, borderpad=0.5, sep=5, |
| 122 | + frameon=False) |
| 123 | + ax.add_artist(asb) |
| 124 | + |
| 125 | + |
75 | 126 | if __name__ == '__main__':
|
76 | 127 | import nose
|
77 | 128 | nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
|
0 commit comments