Skip to content

Fix/restore secondary axis support for Transform-type functions #27267

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

Merged
Merged
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
7 changes: 7 additions & 0 deletions lib/matplotlib/axes/_secondary_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import matplotlib.ticker as mticker
from matplotlib.axes._base import _AxesBase, _TransformedBoundsLocator
from matplotlib.axis import Axis
from matplotlib.transforms import Transform


class SecondaryAxis(_AxesBase):
Expand Down Expand Up @@ -144,11 +145,17 @@ def set_functions(self, functions):
If a transform is supplied, then the transform must have an
inverse.
"""

if (isinstance(functions, tuple) and len(functions) == 2 and
callable(functions[0]) and callable(functions[1])):
# make an arbitrary convert from a two-tuple of functions
# forward and inverse.
self._functions = functions
elif isinstance(functions, Transform):
self._functions = (
functions.transform,
lambda x: functions.inverted().transform(x)
)
elif functions is None:
self._functions = (lambda x: x, lambda x: x)
else:
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/secondary_xy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7494,6 +7494,20 @@ def test_annotate_across_transforms():
arrowprops=dict(arrowstyle="->"))


class _Translation(mtransforms.Transform):
input_dims = 1
output_dims = 1

def __init__(self, dx):
self.dx = dx

def transform(self, values):
return values + self.dx

def inverted(self):
return _Translation(-self.dx)


@image_comparison(['secondary_xy.png'], style='mpl20')
def test_secondary_xy():
fig, axs = plt.subplots(1, 2, figsize=(10, 5), constrained_layout=True)
Expand All @@ -7513,6 +7527,7 @@ def invert(x):
secax(0.4, functions=(lambda x: 2 * x, lambda x: x / 2))
secax(0.6, functions=(lambda x: x**2, lambda x: x**(1/2)))
secax(0.8)
secax("top" if nn == 0 else "right", functions=_Translation(2))


def test_secondary_fail():
Expand Down