|
9 | 9 | from mpl_toolkits.axes_grid1 import host_subplot
|
10 | 10 | from mpl_toolkits.axes_grid1 import make_axes_locatable
|
11 | 11 | from mpl_toolkits.axes_grid1 import AxesGrid
|
12 |
| -from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset |
| 12 | +from mpl_toolkits.axes_grid1.inset_locator import ( |
| 13 | + zoomed_inset_axes, |
| 14 | + mark_inset, |
| 15 | + inset_axes |
| 16 | +) |
13 | 17 | from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
|
14 | 18 |
|
15 | 19 | from matplotlib.colors import LogNorm
|
@@ -155,6 +159,73 @@ def get_demo_image():
|
155 | 159 | ax.add_artist(asb)
|
156 | 160 |
|
157 | 161 |
|
| 162 | +@image_comparison( |
| 163 | + baseline_images=['inset_axes'], style='default', extensions=['png'], |
| 164 | + remove_text=True) |
| 165 | +def test_inset_axes(): |
| 166 | + def get_demo_image(): |
| 167 | + from matplotlib.cbook import get_sample_data |
| 168 | + import numpy as np |
| 169 | + f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) |
| 170 | + z = np.load(f) |
| 171 | + # z is a numpy array of 15x15 |
| 172 | + return z, (-3, 4, -4, 3) |
| 173 | + |
| 174 | + fig, ax = plt.subplots(figsize=[5, 4]) |
| 175 | + |
| 176 | + # prepare the demo image |
| 177 | + Z, extent = get_demo_image() |
| 178 | + Z2 = np.zeros([150, 150], dtype="d") |
| 179 | + ny, nx = Z.shape |
| 180 | + Z2[30:30 + ny, 30:30 + nx] = Z |
| 181 | + |
| 182 | + # extent = [-3, 4, -4, 3] |
| 183 | + ax.imshow(Z2, extent=extent, interpolation="nearest", |
| 184 | + origin="lower") |
| 185 | + |
| 186 | + # creating our inset axes without a bbox_transform parameter |
| 187 | + axins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1)) |
| 188 | + |
| 189 | + axins.imshow(Z2, extent=extent, interpolation="nearest", |
| 190 | + origin="lower") |
| 191 | + axins.yaxis.get_major_locator().set_params(nbins=7) |
| 192 | + axins.xaxis.get_major_locator().set_params(nbins=7) |
| 193 | + # sub region of the original image |
| 194 | + x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 |
| 195 | + axins.set_xlim(x1, x2) |
| 196 | + axins.set_ylim(y1, y2) |
| 197 | + |
| 198 | + plt.xticks(visible=False) |
| 199 | + plt.yticks(visible=False) |
| 200 | + |
| 201 | + # draw a bbox of the region of the inset axes in the parent axes and |
| 202 | + # connecting lines between the bbox and the inset axes area |
| 203 | + mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5") |
| 204 | + |
| 205 | + asb = AnchoredSizeBar(ax.transData, |
| 206 | + 0.5, |
| 207 | + '0.5', |
| 208 | + loc=8, |
| 209 | + pad=0.1, borderpad=0.5, sep=5, |
| 210 | + frameon=False) |
| 211 | + ax.add_artist(asb) |
| 212 | + |
| 213 | + |
| 214 | +def test_inset_axes_without_transform_should_use_parent_axes(): |
| 215 | + # creating our figure |
| 216 | + fig = plt.figure(dpi=150) |
| 217 | + |
| 218 | + # gca method gets current axes of the figure |
| 219 | + ax = plt.gca() |
| 220 | + ax.plot([0.0, 0.25, 0.50, 1.0], [0.1, 0.2, 0.4, 0.9], color='b') |
| 221 | + |
| 222 | + # creating our inset_axes. without a bbox_transform parameter |
| 223 | + ax_ins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1)) |
| 224 | + ax_ins.plot([0.0, 0.25, 0.50, 1.0], [0.9, 0.4, 0.2, 0.1], color='r') |
| 225 | + |
| 226 | + assert ax.transAxes == ax_ins.transAxes |
| 227 | + |
| 228 | + |
158 | 229 | @image_comparison(baseline_images=['zoomed_axes',
|
159 | 230 | 'inverted_zoomed_axes'],
|
160 | 231 | extensions=['png'])
|
|
0 commit comments