Skip to content

Commit bda45ba

Browse files
committed
Remove axis() manual argument parsing.
axis is now just a standard function taking 0-1 arguments. This change *does* make it possible to call `axis(None)` as synonym for `axis()`, as well as passing the sole argument under the name `arg` (unless we decide to switch to positional-only args) but that seems fine.
1 parent 13ab5fc commit bda45ba

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ def apply_aspect(self, position=None):
20642064
x1 = xc + Xsize / 2.0
20652065
self.set_xbound(x_trf.inverted().transform([x0, x1]))
20662066

2067-
def axis(self, *args, emit=True, **kwargs):
2067+
def axis(self, arg=None, *, emit=True, **kwargs):
20682068
"""
20692069
Convenience method to get or set some axis properties.
20702070
@@ -2122,37 +2122,34 @@ def axis(self, *args, emit=True, **kwargs):
21222122
matplotlib.axes.Axes.set_xlim
21232123
matplotlib.axes.Axes.set_ylim
21242124
"""
2125-
if len(args) > 1:
2126-
raise TypeError("axis() takes 0 or 1 positional arguments but "
2127-
f"{len(args)} were given")
2128-
elif len(args) == 1 and isinstance(args[0], (str, bool)):
2129-
s = args[0]
2130-
if s is True:
2131-
s = 'on'
2132-
if s is False:
2133-
s = 'off'
2134-
s = s.lower()
2135-
if s == 'on':
2125+
if isinstance(arg, (str, bool)):
2126+
if arg is True:
2127+
arg = 'on'
2128+
if arg is False:
2129+
arg = 'off'
2130+
arg = arg.lower()
2131+
if arg == 'on':
21362132
self.set_axis_on()
2137-
elif s == 'off':
2133+
elif arg == 'off':
21382134
self.set_axis_off()
2139-
elif s in ('equal', 'tight', 'scaled', 'auto', 'image', 'square'):
2135+
elif arg in [
2136+
'equal', 'tight', 'scaled', 'auto', 'image', 'square']:
21402137
self.set_autoscale_on(True)
21412138
self.set_aspect('auto')
21422139
self.autoscale_view(tight=False)
2143-
if s == 'equal':
2140+
if arg == 'equal':
21442141
self.set_aspect('equal', adjustable='datalim')
2145-
elif s == 'scaled':
2142+
elif arg == 'scaled':
21462143
self.set_aspect('equal', adjustable='box', anchor='C')
21472144
self.set_autoscale_on(False) # Req. by Mark Bakker
2148-
elif s == 'tight':
2145+
elif arg == 'tight':
21492146
self.autoscale_view(tight=True)
21502147
self.set_autoscale_on(False)
2151-
elif s == 'image':
2148+
elif arg == 'image':
21522149
self.autoscale_view(tight=True)
21532150
self.set_autoscale_on(False)
21542151
self.set_aspect('equal', adjustable='box', anchor='C')
2155-
elif s == 'square':
2152+
elif arg == 'square':
21562153
self.set_aspect('equal', adjustable='box', anchor='C')
21572154
self.set_autoscale_on(False)
21582155
xlim = self.get_xlim()
@@ -2163,13 +2160,12 @@ def axis(self, *args, emit=True, **kwargs):
21632160
self.set_ylim([ylim[0], ylim[0] + edge_size],
21642161
emit=emit, auto=False)
21652162
else:
2166-
raise ValueError(f"Unrecognized string {s!r} to axis; "
2163+
raise ValueError(f"Unrecognized string {arg!r} to axis; "
21672164
"try 'on' or 'off'")
21682165
else:
2169-
if len(args) == 1:
2170-
limits = args[0]
2166+
if arg is not None:
21712167
try:
2172-
xmin, xmax, ymin, ymax = limits
2168+
xmin, xmax, ymin, ymax = arg
21732169
except (TypeError, ValueError) as err:
21742170
raise TypeError('the first argument to axis() must be an '
21752171
'iterable of the form '

0 commit comments

Comments
 (0)