22
33import numpy as np
44
5- from matplotlib import _api , _docstring
5+ from matplotlib import _api , _docstring , transforms
66import matplotlib .ticker as mticker
77from matplotlib .axes ._base import _AxesBase , _TransformedBoundsLocator
88from matplotlib .axis import Axis
@@ -14,7 +14,8 @@ class SecondaryAxis(_AxesBase):
1414 General class to hold a Secondary_X/Yaxis.
1515 """
1616
17- def __init__ (self , parent , orientation , location , functions , ** kwargs ):
17+ def __init__ (self , parent , orientation , location , functions , transform = None ,
18+ ** kwargs ):
1819 """
1920 See `.secondary_xaxis` and `.secondary_yaxis` for the doc string.
2021 While there is no need for this to be private, it should really be
@@ -39,7 +40,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
3940 self ._parentscale = None
4041 # this gets positioned w/o constrained_layout so exclude:
4142
42- self .set_location (location )
43+ self .set_location (location , transform )
4344 self .set_functions (functions )
4445
4546 # styling:
@@ -74,7 +75,7 @@ def set_alignment(self, align):
7475 self ._axis .set_ticks_position (align )
7576 self ._axis .set_label_position (align )
7677
77- def set_location (self , location ):
78+ def set_location (self , location , transform = None ):
7879 """
7980 Set the vertical or horizontal location of the axes in
8081 parent-normalized coordinates.
@@ -87,8 +88,17 @@ def set_location(self, location):
8788 orientation='y'. A float indicates the relative position on the
8889 parent Axes to put the new Axes, 0.0 being the bottom (or left)
8990 and 1.0 being the top (or right).
91+
92+ transform : `.Transform`, optional
93+ Transform for the location to use. Defaults to
94+ the parent's ``transAxes``, so locations are normally relative to
95+ the parent axes.
96+
97+ .. versionadded:: 3.9
9098 """
9199
100+ _api .check_isinstance ((transforms .Transform , None ), transform = transform )
101+
92102 # This puts the rectangle into figure-relative coordinates.
93103 if isinstance (location , str ):
94104 _api .check_in_list (self ._locstrings , location = location )
@@ -106,15 +116,28 @@ def set_location(self, location):
106116 # An x-secondary axes is like an inset axes from x = 0 to x = 1 and
107117 # from y = pos to y = pos + eps, in the parent's transAxes coords.
108118 bounds = [0 , self ._pos , 1. , 1e-10 ]
119+
120+ # If a transformation is provided, use its y component rather than
121+ # the parent's transAxes. This can be used to place axes in the data
122+ # coords, for instance.
123+ if transform is not None :
124+ transform = transforms .blended_transform_factory (
125+ self ._parent .transAxes , transform )
109126 else : # 'y'
110127 bounds = [self ._pos , 0 , 1e-10 , 1 ]
128+ if transform is not None :
129+ transform = transforms .blended_transform_factory (
130+ transform , self ._parent .transAxes ) # Use provided x axis
131+
132+ # If no transform is provided, use the parent's transAxes
133+ if transform is None :
134+ transform = self ._parent .transAxes
111135
112136 # this locator lets the axes move in the parent axes coordinates.
113137 # so it never needs to know where the parent is explicitly in
114138 # figure coordinates.
115139 # it gets called in ax.apply_aspect() (of all places)
116- self .set_axes_locator (
117- _TransformedBoundsLocator (bounds , self ._parent .transAxes ))
140+ self .set_axes_locator (_TransformedBoundsLocator (bounds , transform ))
118141
119142 def apply_aspect (self , position = None ):
120143 # docstring inherited.
@@ -278,6 +301,14 @@ def set_color(self, color):
278301 See :doc:`/gallery/subplots_axes_and_figures/secondary_axis`
279302 for examples of making these conversions.
280303
304+ transform : `.Transform`, optional
305+ If specified, *location* will be
306+ placed relative to this transform (in the direction of the axis)
307+ rather than the parent's axis. i.e. a secondary x-axis will
308+ use the provided y transform and the x transform of the parent.
309+
310+ .. versionadded:: 3.9
311+
281312Returns
282313-------
283314ax : axes._secondary_axes.SecondaryAxis
0 commit comments