8000 Merge pull request #24446 from anntzer/axis-argparse · matplotlib/matplotlib@39f40a0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39f40a0

Browse files
authored
Merge pull request #24446 from anntzer/axis-argparse
Remove axis() manual argument parsing.
2 parents 652d317 + d94bc45 commit 39f40a0

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
@@ -2072,7 +2072,7 @@ def apply_aspect(self, position=None):
20722072
x1 = xc + Xsize / 2.0
20732073
self.set_xbound(x_trf.inverted().transform([x0, x1]))
20742074

2075-
def axis(self, *args, emit=True, **kwargs):
2075+
def axis(self, arg=None, /, *, emit=True, **kwargs):
20762076
"""
20772077
Convenience method to get or set some axis properties.
20782078
@@ -2130,37 +2130,34 @@ def axis(self, *args, emit=True, **kwargs):
21302130
matplotlib.axes.Axes.set_xlim
21312131
matplotlib.axes.Axes.set_ylim
21322132
"""
2133-
if len(args) > 1:
2134-
raise TypeError("axis() takes 0 or 1 positional arguments but "
2135-
f"{len(args)} were given")
2136-
elif len(args) == 1 and isinstance(args[0], (str, bool)):
2137-
s = args[0]
2138-
if s is True:
2139-
s = 'on'
2140-
if s is False:
2141-
s = 'off'
2142-
s = s.lower()
2143-
if s == 'on':
2133+
if isinstance(arg, (str, bool)):
2134+
if arg is True:
2135+
arg = 'on'
2136+
if arg is False:
2137+
arg = 'off'
2138+
arg = arg.lower()
2139+
if arg == 'on':
21442140
self.set_axis_on()
2145-
elif s == 'off':
2141+
elif arg == 'off':
21462142
self.set_axis_off()
2147-
elif s in ('equal', 'tight', 'scaled', 'auto', 'image', 'square'):
2143+
elif arg in [
2144+
'equal', 'tight', 'scaled', 'auto', 'image', 'square']:
21482145
self.set_autoscale_on(True)
21492146
self.set_aspect('auto')
21502147
self.autoscale_view(tight=False)
2151-
if s == 'equal':
2148+
if arg == 'equal':
21522149
self.set_aspect('equal', adjustable='datalim')
2153-
elif s == 'scaled':
2150+
elif arg == 'scaled':
21542151
self.set_aspect('equal', adjustable='box', anchor='C')
21552152
self.set_autoscale_on(False) # Req. by Mark Bakker
2156-
elif s == 'tight':
2153+
elif arg == 'tight':
21572154
self.autoscale_view(tight=True)
21582155
self.set_autoscale_on(False)
2159-
elif s == 'image':
2156+
elif arg == 'image':
21602157
self.autoscale_view(tight=True)
21612158
self.set_autoscale_on(False)
21622159
self.set_aspect('equal', adjustable='box', anchor='C')
2163-
elif s == 'square':
2160+
elif arg == 'square':
21642161
self.set_aspect('equal', adjustable='box', anchor='C')
21652162
self.set_autoscale_on(False)
21662163
xlim = self.get_xlim()
@@ -2171,13 +2168,12 @@ def axis(self, *args, emit=True, **kwargs):
21712168
self.set_ylim([ylim[0], ylim[0] + edge_size],
21722169
emit=emit, auto=False)
21732170
else:
2174-
raise ValueError(f"Unrecognized string {s!r} to axis; "
2171+
raise ValueError(f"Unrecognized string {arg!r} to axis; "
21752172
"try 'on' or 'off'")
21762173
else:
2177-
if len(args) == 1:
2178-
limits = args[0]
2174+
if arg is not None:
21792175
try:
2180-
xmin, xmax, ymin, ymax = limits
2176+
xmin, xmax, ymin, ymax = arg
21812177
except (TypeError, ValueError) as err:
21822178
raise TypeError('the first argument to axis() must be an '
21832179
'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
@@ -5432,8 +5432,8 @@ def test_shared_aspect_error():
54325432

54335433
@pytest.mark.parametrize('err, args, kwargs, match',
54345434
((TypeError, (1, 2), {},
5435-
r"axis\(\) takes 0 or 1 positional arguments but 2"
5436-
" were given"),
5435+
r"axis\(\) takes from 0 to 1 positional arguments "
5436+
"but 2 were given"),
54375437
(ValueError, ('foo', ), {},
54385438
"Unrecognized string 'foo' to axis; try 'on' or "
54395439
"'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)
0