Skip to content

Commit e33abe5

Browse files
Move transform type checks and improve docs
1 parent cb55d34 commit e33abe5

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

lib/matplotlib/axes/_axes.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,9 @@ def invert(x):
597597
if not (location in ['top', 'bottom'] or isinstance(location, Real)):
598598
raise ValueError('secondary_xaxis location must be either '
599599
'a float or "top"/"bottom"')
600-
if not (isinstance(transform, mtransforms.Transform) or transform is None):
601-
raise ValueError('transform must be either an mtransforms.Transform '
602-
'or None')
603600

604601
secondary_ax = SecondaryAxis(self, 'x', location, functions,
605-
transform, **kwargs)
602+
transform, **kwargs)
606603
self.add_child_axes(secondary_ax)
607604
return secondary_ax
608605

@@ -628,15 +625,25 @@ def secondary_yaxis(self, location, *, functions=None, transform=None, **kwargs)
628625
secax = ax.secondary_yaxis('right', functions=(np.deg2rad,
629626
np.rad2deg))
630627
secax.set_ylabel('radians')
628+
629+
To add a secondary axis relative to your data, you can pass a transform
630+
to the new axis.
631+
632+
.. plot::
633+
634+
fig, ax = plt.subplots()
635+
ax.plot(range(0, 5), range(-1, 4))
636+
637+
# Pass 'ax.transData' as a transform to place the axis
638+
# relative to your data at x=0
639+
secax = ax.secondary_yaxis(3, transform=ax.transData)
631640
"""
632641
if not (location in ['left', 'right'] or isinstance(location, Real)):
633642
raise ValueError('secondary_xaxis location must be either '
634643
'a float or "left"/"right"')
635-
if not (isinstance(transform, mtransforms.Transform) or transform is None):
636-
raise ValueError('transform must be either an mtransforms.Transform '
637-
'or None')
644+
638645
secondary_ax = SecondaryAxis(self, 'y', location, functions,
639-
transform, **kwargs)
646+
transform, **kwargs)
640647
self.add_child_axes(secondary_ax)
641648
return secondary_ax
642649

lib/matplotlib/axes/_secondary_axes.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,15 @@ def set_location(self, location, transform=None):
9090
9191
transform : `.Transform`, optional
9292
Transform for the location to use. Defaults to
93-
self._parent.transAxes, so locations are normally relative to
93+
the parent's ``transAxes``, so locations are normally relative to
9494
the parent axes.
95+
96+
.. versionadded:: 3.8
9597
"""
9698

99+
# Make sure transform is a Transform or None.
100+
_api.check_isinstance((transforms.Transform, None), transform=transform)
101+
97102
# This puts the rectangle into figure-relative coordinates.
98103
if isinstance(location, str):
99104
_api.check_in_list(self._locstrings, location=location)
@@ -132,8 +137,7 @@ def set_location(self, location, transform=None):
132137
# so it never needs to know where the parent is explicitly in
133138
# figure coordinates.
134139
# it gets called in ax.apply_aspect() (of all places)
135-
self.set_axes_locator(
136-
_TransformedBoundsLocator(bounds, transform))
140+
self.set_axes_locator(_TransformedBoundsLocator(bounds, transform))
137141

138142
def apply_aspect(self, position=None):
139143
# docstring inherited.
@@ -281,12 +285,6 @@ def set_color(self, color):
281285
parent axes to put the new axes, 0.0 being the bottom (or left)
282286
and 1.0 being the top (or right).
283287
284-
transform : `.Transform`, optional
285-
An optional transform that can be passed. *location* will be
286-
placed relative to this transform (in the direction of the axis)
287-
rather than the parent's axis. i.e. a secondary x-axis will
288-
use the provided y transform and the x transform of the parent.
289-
290288
functions : 2-tuple of func, or Transform with an inverse
291289
292290
If a 2-tuple of functions, the user specifies the transform
@@ -301,6 +299,14 @@ def set_color(self, color):
301299
See :doc:`/gallery/subplots_axes_and_figures/secondary_axis`
302300
for examples of making these conversions.
303301
302+
transform : `.Transform`, optional
303+
An optional transform that can be passed. *location* will be
304+
placed relative to this transform (in the direction of the axis)
305+
rather than the parent's axis. i.e. a secondary x-axis will
306+
use the provided y transform and the x transform of the parent.
307+
308+
.. versionadded:: 3.8
309+
304310
Returns
305311
-------
306312
ax : axes._secondary_axes.SecondaryAxis

lib/matplotlib/tests/test_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7241,7 +7241,7 @@ def test_secondary_fail():
72417241
ax.secondary_xaxis('right')
72427242
with pytest.raises(ValueError):
72437243
ax.secondary_yaxis('bottom')
7244-
with pytest.raises(ValueError):
7244+
with pytest.raises(TypeError):
72457245
ax.secondary_xaxis(0.2, transform='error')
72467246

72477247

0 commit comments

Comments
 (0)