1
+ import numbers
2
+
1
3
import numpy as np
2
4
3
5
from matplotlib import _api , _docstring
@@ -17,7 +19,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
17
19
While there is no need for this to be private, it should really be
18
20
called by those higher level functions.
19
21
"""
20
-
22
+ _api . check_in_list ([ "x" , "y" ], orientation = orientation )
21
23
self ._functions = functions
22
24
self ._parent = parent
23
25
self ._orientation = orientation
@@ -28,7 +30,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
28
30
self ._axis = self .xaxis
29
31
self ._locstrings = ['top' , 'bottom' ]
30
32
self ._otherstrings = ['left' , 'right' ]
31
- elif self . _orientation == 'y' :
33
+ else : # 'y'
32
34
super ().__init__ (self ._parent .figure , [0 , 1. , 0.0001 , 1 ], ** kwargs )
33
35
self ._axis = self .yaxis
34
36
self ._locstrings = ['right' , 'left' ]
@@ -40,11 +42,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
40
42
self .set_functions (functions )
41
43
42
44
# styling:
43
- if self ._orientation == 'x' :
44
- otheraxis = self .yaxis
45
- else :
46
- otheraxis = self .xaxis
47
-
45
+ otheraxis = self .yaxis if self ._orientation == 'x' else self .xaxis
48
46
otheraxis .set_major_locator (mticker .NullLocator ())
49
47
otheraxis .set_ticks_position ('none' )
50
48
@@ -63,8 +61,8 @@ def set_alignment(self, align):
63
61
64
62
Parameters
65
63
----------
66
- align : str
67
- either 'top' or 'bottom' for orientation='x' or
64
+ align : {'top', 'bottom', 'left', 'right'}
65
+ Either 'top' or 'bottom' for orientation='x' or
68
66
'left' or 'right' for orientation='y' axis.
69
67
"""
70
68
_api .check_in_list (self ._locstrings , align = align )
@@ -92,23 +90,22 @@ def set_location(self, location):
92
90
93
91
# This puts the rectangle into figure-relative coordinates.
94
92
if isinstance (location , str ):
95
- if location in ['top' , 'right' ]:
96
- self ._pos = 1.
97
- elif location in ['bottom' , 'left' ]:
98
- self ._pos = 0.
99
- else :
100
- raise ValueError (
101
- f"location must be { self ._locstrings [0 ]!r} , "
102
- f"{ self ._locstrings [1 ]!r} , or a float, not { location !r} " )
103
- else :
93
+ _api .check_in_list (self ._locstrings , location = location )
94
+ self ._pos = 1. if location in ('top' , 'right' ) else 0.
95
+ elif isinstance (location , numbers .Real ):
104
96
self ._pos = location
97
+ else :
98
+ raise ValueError (
99
+ f"location must be { self ._locstrings [0 ]!r} , "
100
+ f"{ self ._locstrings [1 ]!r} , or a float, not { location !r} " )
101
+
105
102
self ._loc = location
106
103
107
104
if self ._orientation == 'x' :
108
105
# An x-secondary axes is like an inset axes from x = 0 to x = 1 and
109
106
# from y = pos to y = pos + eps, in the parent's transAxes coords.
110
107
bounds = [0 , self ._pos , 1. , 1e-10 ]
111
- else :
108
+ else : # 'y'
112
109
bounds = [self ._pos , 0 , 1e-10 , 1 ]
113
110
114
111
# this locator lets the axes move in the parent axes coordinates.
@@ -161,9 +158,7 @@ def set_functions(self, functions):
161
158
'and the second being the inverse' )
162
159
self ._set_scale ()
163
160
164
- # Should be changed to draw(self, renderer) once the deprecation of
165
- # renderer=None and of inframe expires.
166
- def draw (self , * args , ** kwargs ):
161
+ def draw (self , renderer ):
167
162
"""
168
163
Draw the secondary axes.
169
164
@@ -175,7 +170,7 @@ def draw(self, *args, **kwargs):
175
170
self ._set_lims ()
176
171
# this sets the scale in case the parent has set its scale.
177
172
self ._set_scale ()
178
- super ().draw (* args , ** kwargs )
173
+ super ().draw (renderer )
179
174
180
175
def _set_scale (self ):
181
176
"""
@@ -185,22 +180,18 @@ def _set_scale(self):
185
180
if self ._orientation == 'x' :
186
181
pscale = self ._parent .xaxis .get_scale ()
187
182
set_scale = self .set_xscale
188
- if self . _orientation == 'y' :
183
+ else : # 'y'
189
184
pscale = self ._parent .yaxis .get_scale ()
190
185
set_scale = self .set_yscale
191
186
if pscale == self ._parentscale :
192
187
return
193
188
194
- if pscale == 'log' :
195
- defscale = 'functionlog'
196
- else :
197
- defscale = 'function'
198
-
199
189
if self ._ticks_set :
200
190
ticks = self ._axis .get_ticklocs ()
201
191
202
192
# need to invert the roles here for the ticks to line up.
203
- set_scale (defscale , functions = self ._functions [::- 1 ])
193
+ set_scale ('functionlog' if pscale == 'log' else 'function' ,
194
+ functions = self ._functions [::- 1 ])
204
195
205
196
# OK, set_scale sets the locators, but if we've called
206
197
# axsecond.set_ticks, we want to keep those.
@@ -218,7 +209,7 @@ def _set_lims(self):
218
209
if self ._orientation == 'x' :
219
210
lims = self ._parent .get_xlim ()
220
211
set_lim = self .set_xlim
221
- if self . _orientation == 'y' :
212
+ else : # 'y'
222
213
lims = self ._parent .get_ylim ()
223
214
set_lim = self .set_ylim
224
215
order = lims [0 ] < lims [1 ]
@@ -249,7 +240,7 @@ def set_color(self, color):
249
240
self .spines .bottom .set_color (color )
250
241
self .spines .top .set_color (color )
251
242
self .xaxis .label .set_color (color )
252
- else :
243
+ else : # 'y'
253
244
self .tick_params (axis = 'y' , colors = color )
254
245
self .spines .left .set_color (color )
255
246
self .spines .right .set_color (color )
0 commit comments