Skip to content

Commit ed46a19

Browse files
committed
add secondary axis support for Transform-type functions
1 parent 4524fdb commit ed46a19

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 5 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,15 @@ 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, functions.inverted().transform)
152157
elif functions is None:
153158
self._functions = (lambda x: x, lambda x: x)
154159
else:
3.71 KB
Loading

lib/matplotlib/tests/test_axes.py

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

74967496

7497+
class _Translation(mtransforms.Transform):
7498+
input_dims = 1
7499+
output_dims = 1
7500+
7501+
def __init__(self, dx):
7502+
self.dx = dx
7503+
7504+
def transform(self, values):
7505+
return values + self.dx
7506+
7507+
def inverted(self):
7508+
return _Translation(-self.dx)
7509+
7510+
74977511
@image_comparison(['secondary_xy.png'], style='mpl20')
74987512
def test_secondary_xy():
74997513
fig, axs = plt.subplots(1, 2, figsize=(10, 5), constrained_layout=True)
@@ -7513,6 +7527,7 @@ def invert(x):
75137527
secax(0.4, functions=(lambda x: 2 * x, lambda x: x / 2))
75147528
secax(0.6, functions=(lambda x: x**2, lambda x: x**(1/2)))
75157529
secax(0.8)
7530+
secax("top" if nn == 0 else "right", functions=_Translation(2))
75167531

75177532

75187533
def test_secondary_fail():

0 commit comments

Comments
 (0)