|
37 | 37 | _log = logging.getLogger(__name__)
|
38 | 38 |
|
39 | 39 |
|
40 |
| -def _make_inset_locator(bounds, trans, parent): |
| 40 | +class _InsetLocator: |
41 | 41 | """
|
42 |
| - Helper function to locate inset axes, used in |
43 |
| - `.Axes.inset_axes`. |
| 42 | + Axes locator for `.Axes.inset_axes`. |
44 | 43 |
|
45 |
| - A locator gets used in `Axes.set_aspect` to override the default |
46 |
| - locations... It is a function that takes an axes object and |
47 |
| - a renderer and tells `set_aspect` where it is to be placed. |
48 |
| -
|
49 |
| - Here *rect* is a rectangle [l, b, w, h] that specifies the |
50 |
| - location for the axes in the transform given by *trans* on the |
51 |
| - *parent*. |
| 44 | + The locater is a callable object used in `.Axes.set_aspect` to compute the |
| 45 | + axes location depending on the renderer. |
52 | 46 | """
|
53 |
| - _bounds = mtransforms.Bbox.from_bounds(*bounds) |
54 |
| - _trans = trans |
55 |
| - _parent = parent |
56 | 47 |
|
57 |
| - def inset_locator(ax, renderer): |
58 |
| - bbox = _bounds |
59 |
| - bb = mtransforms.TransformedBbox(bbox, _trans) |
60 |
| - tr = _parent.figure.transFigure.inverted() |
61 |
| - bb = mtransforms.TransformedBbox(bb, tr) |
62 |
| - return bb |
| 48 | + def __init__(self, bounds, transform): |
| 49 | + """ |
| 50 | + *bounds* (a ``[l, b, w, h]`` rectangle) and *transform* together |
| 51 | + specify the position of the inset axes. |
| 52 | + """ |
| 53 | + self._bounds = bounds |
| 54 | + self._transform = transform |
63 | 55 |
|
64 |
| - return inset_locator |
| 56 | + def __call__(self, ax, renderer): |
| 57 | + # Subtracting transFigure will typically rely on inverted(), freezing |
| 58 | + # the transform; thus, this needs to be delayed until draw time as |
| 59 | + # transFigure may otherwise change after this is evaluated. |
| 60 | + return mtransforms.TransformedBbox( |
| 61 | + mtransforms.Bbox.from_bounds(*self._bounds), |
| 62 | + self._transform - ax.figure.transFigure) |
65 | 63 |
|
66 | 64 |
|
67 | 65 | # The axes module contains all the wrappers to plotting functions.
|
@@ -468,10 +466,9 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
|
468 | 466 | kwargs.setdefault('label', 'inset_axes')
|
469 | 467 |
|
470 | 468 | # This puts the rectangle into figure-relative coordinates.
|
471 |
| - inset_locator = _make_inset_locator(bounds, transform, self) |
472 |
| - bb = inset_locator(None, None) |
473 |
| - |
474 |
| - inset_ax = Axes(self.figure, bb.bounds, zorder=zorder, **kwargs) |
| 469 | + inset_locator = _InsetLocator(bounds, transform) |
| 470 | + bounds = inset_locator(self, None).bounds |
| 471 | + inset_ax = Axes(self.figure, bounds, zorder=zorder, **kwargs) |
475 | 472 | # this locator lets the axes move if in data coordinates.
|
476 | 473 | # it gets called in `ax.apply_aspect() (of all places)
|
477 | 474 | inset_ax.set_axes_locator(inset_locator)
|
|
0 commit comments