Skip to content

Reuse inset_axes locator machinery for secondary_axes. #17249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ def _make_inset_locator(bounds, trans, parent):
location for the axes in the transform given by *trans* on the
*parent*.
"""
_bounds = mtransforms.Bbox.from_bounds(*bounds)
_trans = trans
_parent = parent
bbox = mtransforms.Bbox.from_bounds(*bounds)

def inset_locator(ax, renderer):
bbox = _bounds
bb = mtransforms.TransformedBbox(bbox, _trans)
tr = _parent.figure.transFigure.inverted()
bb = mtransforms.TransformedBbox(bb, tr)
return bb
# Subtracting transFigure will typically rely on inverted(), freezing
# the transform; thus, this needs to be delayed until draw time as
# transFigure may otherwise change after this is evaluated.
return mtransforms.TransformedBbox(
bbox, trans - parent.figure.transFigure)

return inset_locator

Expand Down
33 changes: 5 additions & 28 deletions lib/matplotlib/axes/_secondary_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,10 @@
import matplotlib.docstring as docstring
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
from matplotlib.axes import _axes
from matplotlib.axes._base import _AxesBase


def _make_secondary_locator(rect, parent):
"""
Helper function to locate the secondary axes.

A locator gets used in `Axes.set_aspect` to override the default
locations... It is a function that takes an axes object and
a renderer and tells `set_aspect` where it is to be placed.

This locator make the transform be in axes-relative co-coordinates
because that is how we specify the "location" of the secondary axes.

Here *rect* is a rectangle [l, b, w, h] that specifies the
location for the axes in the transform given by *trans* on the
*parent*.
"""
_rect = mtransforms.Bbox.from_bounds(*rect)
def secondary_locator(ax, renderer):
# delay evaluating transform until draw time because the
# parent transform may have changed (i.e. if window reesized)
bb = mtransforms.TransformedBbox(_rect, parent.transAxes)
tr = parent.figure.transFigure.inverted()
bb = mtransforms.TransformedBbox(bb, tr)
return bb

return secondary_locator


class SecondaryAxis(_AxesBase):
"""
General class to hold a Secondary_X/Yaxis.
Expand Down Expand Up @@ -137,11 +111,14 @@ def set_location(self, location):
self._loc = location

if self._orientation == 'x':
# An x-secondary axes is like an inset axes from x = 0 to x = 1 and
# from y = pos to y = pos + eps, in the parent's transAxes coords.
bounds = [0, self._pos, 1., 1e-10]
else:
bounds = [self._pos, 0, 1e-10, 1]

secondary_locator = _make_secondary_locator(bounds, self._parent)
secondary_locator = _axes._make_inset_locator(
bounds, self._parent.transAxes, self._parent)

# this locator lets the axes move in the parent axes coordinates.
# so it never needs to know where the parent is explicitly in
Expand Down