2
2
3
3
import numpy as np
4
4
5
- from matplotlib import _api , _docstring
5
+ from matplotlib import _api , _docstring , transforms
6
6
import matplotlib .ticker as mticker
7
7
from matplotlib .axes ._base import _AxesBase , _TransformedBoundsLocator
8
8
from matplotlib .axis import Axis
@@ -14,7 +14,8 @@ class SecondaryAxis(_AxesBase):
14
14
General class to hold a Secondary_X/Yaxis.
15
15
"""
16
16
17
- def __init__ (self , parent , orientation , location , functions , ** kwargs ):
17
+ def __init__ (self , parent , orientation , location , functions , transform = None ,
18
+ ** kwargs ):
18
19
"""
19
20
See `.secondary_xaxis` and `.secondary_yaxis` for the doc string.
20
21
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):
39
40
self ._parentscale = None
40
41
# this gets positioned w/o constrained_layout so exclude:
41
42
42
- self .set_location (location )
43
+ self .set_location (location , transform )
43
44
self .set_functions (functions )
44
45
45
46
# styling:
@@ -74,7 +75,7 @@ def set_alignment(self, align):
74
75
self ._axis .set_ticks_position (align )
75
76
self ._axis .set_label_position (align )
76
77
77
- def set_location (self , location ):
78
+ def set_location (self , location , transform = None ):
78
79
"""
79
80
Set the vertical or horizontal location of the axes in
80
81
parent-normalized coordinates.
@@ -87,8 +88,17 @@ def set_location(self, location):
87
88
orientation='y'. A float indicates the relative position on the
88
89
parent Axes to put the new Axes, 0.0 being the bottom (or left)
89
90
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
90
98
"""
91
99
100
+ _api .check_isinstance ((transforms .Transform , None ), transform = transform )
101
+
92
102
# This puts the rectangle into figure-relative coordinates.
93
103
if isinstance (location , str ):
94
104
_api .check_in_list (self ._locstrings , location = location )
@@ -106,15 +116,28 @@ def set_location(self, location):
106
116
# An x-secondary axes is like an inset axes from x = 0 to x = 1 and
107
117
# from y = pos to y = pos + eps, in the parent's transAxes coords.
108
118
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 )
109
126
else : # 'y'
110
127
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
111
135
112
136
# this locator lets the axes move in the parent axes coordinates.
113
137
# so it never needs to know where the parent is explicitly in
114
138
# figure coordinates.
115
139
# 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 ))
118
141
119
142
def apply_aspect (self , position = None ):
120
143
# docstring inherited.
@@ -278,6 +301,14 @@ def set_color(self, color):
278
301
See :doc:`/gallery/subplots_axes_and_figures/secondary_axis`
279
302
for examples of making these conversions.
280
303
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
+
281
312
Returns
282
313
-------
283
314
ax : axes._secondary_axes.SecondaryAxis
0 commit comments