Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b90aba4

Browse files
authored
Merge pull request #24091 from oscargus/secondaryaxescleanup
MNT: Clean up code in SecondaryAxis
2 parents 94b993a + 8387676 commit b90aba4

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numbers
2+
13
import numpy as np
24

35
from matplotlib import _api, _docstring
@@ -17,7 +19,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
1719
While there is no need for this to be private, it should really be
1820
called by those higher level functions.
1921
"""
20-
22+
_api.check_in_list(["x", "y"], orientation=orientation)
2123
self._functions = functions
2224
self._parent = parent
2325
self._orientation = orientation
@@ -28,7 +30,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
2830
self._axis = self.xaxis
2931
self._locstrings = ['top', 'bottom']
3032
self._otherstrings = ['left', 'right']
31-
elif self._orientation == 'y':
33+
else: # 'y'
3234
super().__init__(self._parent.figure, [0, 1., 0.0001, 1], **kwargs)
3335
self._axis = self.yaxis
3436
self._locstrings = ['right', 'left']
@@ -40,11 +42,7 @@ def __init__(self, parent, orientation, location, functions, **kwargs):
4042
self.set_functions(functions)
4143

4244
# 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
4846
otheraxis.set_major_locator(mticker.NullLocator())
4947
otheraxis.set_ticks_position('none')
5048

@@ -63,8 +61,8 @@ def set_alignment(self, align):
6361
6462
Parameters
6563
----------
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
6866
'left' or 'right' for orientation='y' axis.
6967
"""
7068
_api.check_in_list(self._locstrings, align=align)
@@ -92,23 +90,22 @@ def set_location(self, location):
9290

9391
# This puts the rectangle into figure-relative coordinates.
9492
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):
10496
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+
105102
self._loc = location
106103

107104
if self._orientation == 'x':
108105
# An x-secondary axes is like an inset axes from x = 0 to x = 1 and
109106
# from y = pos to y = pos + eps, in the parent's transAxes coords.
110107
bounds = [0, self._pos, 1., 1e-10]
111-
else:
108+
else: # 'y'
112109
bounds = [self._pos, 0, 1e-10, 1]
113110

114111
# this locator lets the axes move in the parent axes coordinates.
@@ -161,9 +158,7 @@ def set_functions(self, functions):
161158
'and the second being the inverse')
162159
self._set_scale()
163160

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):
167162
"""
168163
Draw the secondary axes.
169164
@@ -175,7 +170,7 @@ def draw(self, *args, **kwargs):
175170
self._set_lims()
176171
# this sets the scale in case the parent has set its scale.
177172
self._set_scale()
178-
super().draw(*args, **kwargs)
173+
super().draw(renderer)
179174

180175
def _set_scale(self):
181176
"""
@@ -185,22 +180,18 @@ def _set_scale(self):
185180
if self._orientation == 'x':
186181
pscale = self._parent.xaxis.get_scale()
187182
set_scale = self.set_xscale
188-
if self._orientation == 'y':
183+
else: # 'y'
189184
pscale = self._parent.yaxis.get_scale()
190185
set_scale = self.set_yscale
191186
if pscale == self._parentscale:
192187
return
193188

194-
if pscale == 'log':
195-
defscale = 'functionlog'
196-
else:
197-
defscale = 'function'
198-
199189
if self._ticks_set:
200190
ticks = self._axis.get_ticklocs()
201191

202192
# 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])
204195

205196
# OK, set_scale sets the locators, but if we've called
206197
# axsecond.set_ticks, we want to keep those.
@@ -218,7 +209,7 @@ def _set_lims(self):
218209
if self._orientation == 'x':
219210
lims = self._parent.get_xlim()
220211
set_lim = self.set_xlim
221-
if self._orientation == 'y':
212+
else: # 'y'
222213
lims = self._parent.get_ylim()
223214
set_lim = self.set_ylim
224215
order = lims[0] < lims[1]
@@ -249,7 +240,7 @@ def set_color(self, color):
249240
self.spines.bottom.set_color(color)
250241
self.spines.top.set_color(color)
251242
self.xaxis.label.set_color(color)
252-
else:
243+
else: # 'y'
253244
self.tick_params(axis='y', colors=color)
254245
self.spines.left.set_color(color)
255246
self.spines.right.set_color(color)

0 commit comments

Comments
 (0)