Skip to content

Commit d94bc45

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 d94bc45

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
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 '

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,8 +2355,8 @@ def axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs):
23552355

23562356
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
23572357
@_copy_docstring_and_deprecators(Axes.axis)
2358-
def axis(*args, emit=True, **kwargs):
2359-
return gca().axis(*args, emit=emit, **kwargs)
2358+
def axis(arg=None, /, *, emit=True, **kwargs):
2359+
return gca().axis(arg, emit=emit, **kwargs)
23602360

23612361

23622362
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5429,8 +5429,8 @@ def test_shared_aspect_error():
54295429

54305430
@pytest.mark.parametrize('err, args, kwargs, match',
54315431
((TypeError, (1, 2), {},
5432-
r"axis\(\) takes 0 or 1 positional arguments but 2"
5433-
" were given"),
5432+
r"axis\(\) takes from 0 to 1 positional arguments "
5433+
"but 2 were given"),
54345434
(ValueError, ('foo', ), {},
54355435
"Unrecognized string 'foo' to axis; try 'on' or "
54365436
"'off'"),

tools/boilerplate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,12 @@ def generate_function(name, called_fullname, template, **kwargs):
182182
if param.kind in [
183183
Parameter.POSITIONAL_OR_KEYWORD,
184184
Parameter.KEYWORD_ONLY] else
185+
'{0}'
186+
if param.kind is Parameter.POSITIONAL_ONLY else
185187
'*{0}'
186188
if param.kind is Parameter.VAR_POSITIONAL else
187189
'**{0}'
188190
if param.kind is Parameter.VAR_KEYWORD else
189-
# Intentionally crash for Parameter.POSITIONAL_ONLY.
190191
None).format(param.name)
191192
for param in params) + ')'
192193
MAX_CALL_PREFIX = 18 # len(' __ret = gca().')

0 commit comments

Comments
 (0)