8000 Merge pull request #27267 from zachjweiner/secondary-axes-support-tra… · matplotlib/matplotlib@b2e3e8b · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b2e3e8b

Browse files
authored
Merge pull request #27267 from zachjweiner/secondary-axes-support-transforms
Fix/restore secondary axis support for Transform-type functions
2 parents f5741e4 + 2803407 commit b2e3e8b

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import matplotlib.ticker as mticker
77
from matplotlib.axes._base import _AxesBase, _TransformedBoundsLocator
88
from matplotlib.axis import Axis
9+
from matplotlib.transforms import Transform
910

1011

1112
class SecondaryAxis(_AxesBase):
@@ -144,11 +145,17 @@ def set_functions(self, functions):
144145
If a transform is supplied, then the transform must have an
145146
inverse.
146147
"""
148+
147149
if (isinstance(functions, tuple) and len(functions) == 2 and
148150
callable(functions[0]) and callable(functions[1])):
149151
# make an arbitrary convert from a two-tuple of functions
150152
# forward and inverse.
151153
self._functions = functions
154+
elif isinstance(functions, Transform):
155+
self._functions = (
156+
functions.transform,
157+
lambda x: functions.inverted().transform(x)
158+
)
152159
elif functions is None:
153160
self._functions = (lambda x: x, lambda x: x)
154161
else:
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7509,6 +7509,20 @@ def test_annotate_across_transforms():
75097509
arrowprops=dict(arrowstyle="->"))
75107510

75117511

7512+
class _Translation(mtransforms.Transform):
7513+
input_dims = 1
7514+
output_dims = 1
7515+
7516+
def __init__(self, dx):
7517+
self.dx = dx
7518+
7519+
def transform(self, values):
7520+
return values + self.dx
7521+
7522+
def inverted(self):
7523+
return _Translation(-self.dx)
7524+
7525+
75127526
@image_comparison(['secondary_xy.png'], style='mpl20')
75137527
def test_secondary_xy():
75147528
fig, axs = plt.subplots(1, 2, figsize=(10, 5), constrained_layout=True)
@@ -7528,6 +7542,7 @@ def invert(x):
75287542
secax(0.4, functions=(lambda x: 2 * x, lambda x: x / 2))
75297543
secax(0.6, functions=(lambda x: x**2, lambda x: x**(1/2)))
75307544
secax(0.8)
7545+
secax("top" if nn == 0 else "right", functions=_Translation(2))
75317546

75327547

75337548
def test_secondary_fail():

0 commit comments

Comments
 (0)
0