Skip to content

Commit 36d7598

Browse files
committed
Add custom _TransformedBoundsLocator class
1 parent 76370b6 commit 36d7598

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

proplot/axes/base.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -644,13 +644,29 @@
644644
docstring._snippet_manager['axes.legend_kwargs'] = _legend_kwargs_docstring
645645

646646

647+
class _TransformedBoundsLocator:
648+
"""
649+
Axes locator for `~Axes.inset_axes` and other axes.
650+
"""
651+
def __init__(self, bounds, transform):
652+
self._bounds = bounds
653+
self._transform = transform
654+
655+
def __call__(self, ax, renderer): # noqa: U100
656+
bbox = mtransforms.Bbox.from_bounds(*self._bounds)
657+
bbox = mtransforms.TransformedBbox(bbox, self._transform)
658+
bbox = mtransforms.TransformedBbox(bbox, ax.figure.transFigure.inverted())
659+
return bbox
660+
661+
647662
class Axes(maxes.Axes):
648663
"""
649664
The lowest-level `~matplotlib.axes.Axes` subclass used by proplot.
650665
Implements basic universal features.
651666
"""
652667
_name = None # derived must override
653668
_name_aliases = ()
669+
_make_inset_locator = _TransformedBoundsLocator
654670

655671
def __repr__(self):
656672
# Show the position in the geometry excluding panels. Panels are
@@ -664,15 +680,15 @@ def __repr__(self):
664680
if self._name in ('cartopy', 'basemap'):
665681
name = name.replace('_' + self._name.title(), 'Geo')
666682
params['backend'] = self._name
667-
if self._colorbar_fill:
668-
name = re.sub('Axes(Subplot)?', 'AxesFill', name)
669-
params['orientation'] = self._colorbar_fill.orientation
670683
if self._inset_parent:
671684
name = re.sub('Axes(Subplot)?', 'AxesInset', name)
672685
params['bounds'] = tuple(np.round(self._inset_bounds, 2))
673686
if self._altx_parent or self._alty_parent:
674687
name = re.sub('Axes(Subplot)?', 'AxesTwin', name)
675688
params['axis'] = 'x' if self._altx_parent else 'y'
689+
if self._colorbar_fill:
690+
name = re.sub('Axes(Subplot)?', 'AxesFill', name)
691+
params['side'] = self._axes._panel_side
676692
if self._panel_side:
677693
name = re.sub('Axes(Subplot)?', 'AxesPanel', name)
678694
params['side'] = self._panel_side
@@ -865,7 +881,7 @@ def _add_inset_axes(
865881
# Converting transform to figure-relative coordinates
866882
transform = self._get_transform(transform, 'axes')
867883
locator = self._make_inset_locator(bounds, transform)
868-
bbox = locator(None, None)
884+
bounds = locator(self, None).bounds
869885
label = kwargs.pop('label', 'inset_axes')
870886
zorder = _not_none(zorder, 4)
871887

@@ -882,7 +898,7 @@ def _add_inset_axes(
882898
# Create axes and apply locator. The locator lets the axes adjust
883899
# automatically if we used data coords. Called by ax.apply_aspect()
884900
cls = mprojections.get_projection_class(kwargs.pop('projection'))
885-
ax = cls(self.figure, bbox.bounds, zorder=zorder, label=label, **kwargs)
901+
ax = cls(self.figure, bounds, zorder=zorder, label=label, **kwargs)
886902
ax.set_axes_locator(locator)
887903
ax._inset_parent = self
888904
ax._inset_bounds = bounds
@@ -1618,17 +1634,6 @@ def _get_transform(self, transform, default='data'):
16181634
else:
16191635
raise ValueError(f'Unknown transform {transform!r}.')
16201636

1621-
def _make_inset_locator(self, bounds, trans):
1622-
"""
1623-
Return a locator that determines child axes bounds.
1624-
"""
1625-
def _inset_locator(ax, renderer):
1626-
bbox = mtransforms.Bbox.from_bounds(*bounds)
1627-
bbox = mtransforms.TransformedBbox(bbox, trans)
1628-
bbox = mtransforms.TransformedBbox(bbox, self.figure.transFigure.inverted())
1629-
return bbox
1630-
return _inset_locator
1631-
16321637
def _register_guide(self, guide, obj, loc, **kwargs):
16331638
"""
16341639
Queue up or replace objects for legends and list-of-artist style colorbars.
@@ -1864,7 +1869,7 @@ def _parse_colorbar_filled(
18641869
# Add the axes as a child of the original axes
18651870
cls = mprojections.get_projection_class('proplot_cartesian')
18661871
locator = self._make_inset_locator(bounds, self.transAxes)
1867-
ax = cls(self.figure, locator(None, None).bounds, zorder=5)
1872+
ax = cls(self.figure, locator(self, None).bounds, zorder=5)
18681873
self.add_child_axes(ax)
18691874
ax.set_axes_locator(locator)
18701875
ax.patch.set_facecolor('none') # ignore axes.alpha application
@@ -1933,7 +1938,7 @@ def _parse_colorbar_inset(
19331938
# Make axes and frame with zorder matching default legend zorder
19341939
cls = mprojections.get_projection_class('proplot_cartesian')
19351940
locator = self._make_inset_locator(bounds_inset, self.transAxes)
1936-
ax = cls(self.figure, locator(None, None).bounds, zorder=5)
1941+
ax = cls(self.figure, locator(self, None).bounds, zorder=5)
19371942
ax.patch.set_facecolor('none')
19381943
ax.set_axes_locator(locator)
19391944
self.add_child_axes(ax)

proplot/axes/cartesian.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def _add_alt(self, sx, **kwargs):
419419
if 'sharex' in kwargs and 'sharey' in kwargs:
420420
raise ValueError('Twinned axes may share only one axis.')
421421
locator = self._make_inset_locator([0, 0, 1, 1], self.transAxes)
422-
ax = CartesianAxes(self.figure, self.get_position(True), **kwargs)
422+
ax = CartesianAxes(self.figure, locator(self, None).bounds, **kwargs)
423423
ax.set_axes_locator(locator)
424424
ax.set_adjustable('datalim')
425425
self.add_child_axes(ax) # to facilitate tight layout

0 commit comments

Comments
 (0)