Skip to content

Remove axis() manual argument parsing. #24446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ def apply_aspect(self, position=None):
x1 = xc + Xsize / 2.0
self.set_xbound(x_trf.inverted().transform([x0, x1]))

def axis(self, *args, emit=True, **kwargs):
def axis(self, arg=None, /, *, emit=True, **kwargs):
"""
Convenience method to get or set some axis properties.

Expand Down Expand Up @@ -2122,37 +2122,34 @@ def axis(self, *args, emit=True, **kwargs):
matplotlib.axes.Axes.set_xlim
matplotlib.axes.Axes.set_ylim
"""
if len(args) > 1:
raise TypeError("axis() takes 0 or 1 positional arguments but "
f"{len(args)} were given")
elif len(args) == 1 and isinstance(args[0], (str, bool)):
s = args[0]
if s is True:
s = 'on'
if s is False:
s = 'off'
s = s.lower()
if s == 'on':
if isinstance(arg, (str, bool)):
if arg is True:
arg = 'on'
if arg is False:
arg = 'off'
arg = arg.lower()
if arg == 'on':
self.set_axis_on()
elif s == 'off':
elif arg == 'off':
self.set_axis_off()
elif s in ('equal', 'tight', 'scaled', 'auto', 'image', 'square'):
elif arg in [
'equal', 'tight', 'scaled', 'auto', 'image', 'square']:
self.set_autoscale_on(True)
self.set_aspect('auto')
self.autoscale_view(tight=False)
if s == 'equal':
if arg == 'equal':
self.set_aspect('equal', adjustable='datalim')
elif s == 'scaled':
elif arg == 'scaled':
self.set_aspect('equal', adjustable='box', anchor='C')
self.set_autoscale_on(False) # Req. by Mark Bakker
elif s == 'tight':
elif arg == 'tight':
self.autoscale_view(tight=True)
self.set_autoscale_on(False)
elif s == 'image':
elif arg == 'image':
self.autoscale_view(tight=True)
self.set_autoscale_on(False)
self.set_aspect('equal', adjustable='box', anchor='C')
elif s == 'square':
elif arg == 'square':
self.set_aspect('equal', adjustable='box', anchor='C')
self.set_autoscale_on(False)
xlim = self.get_xlim()
Expand All @@ -2163,13 +2160,12 @@ def axis(self, *args, emit=True, **kwargs):
self.set_ylim([ylim[0], ylim[0] + edge_size],
emit=emit, auto=False)
else:
raise ValueError(f"Unrecognized string {s!r} to axis; "
raise ValueError(f"Unrecognized string {arg!r} to axis; "
"try 'on' or 'off'")
else:
if len(args) == 1:
limits = args[0]
if arg is not None:
try:
xmin, xmax, ymin, ymax = limits
xmin, xmax, ymin, ymax = arg
except (TypeError, ValueError) as err:
raise TypeError('the first argument to axis() must be an '
'iterable of the form '
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,8 +2355,8 @@ def axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs):

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


# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5429,8 +5429,8 @@ def test_shared_aspect_error():

@pytest.mark.parametrize('err, args, kwargs, match',
((TypeError, (1, 2), {},
r"axis\(\) takes 0 or 1 positional arguments but 2"
" were given"),
r"axis\(\) takes from 0 to 1 positional arguments "
"but 2 were given"),
(ValueError, ('foo', ), {},
"Unrecognized string 'foo' to axis; try 'on' or "
"'off'"),
Expand Down
3 changes: 2 additions & 1 deletion tools/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ def generate_function(name, called_fullname, template, **kwargs):
if param.kind in [
Parameter.POSITIONAL_OR_KEYWORD,
Parameter.KEYWORD_ONLY] else
'{0}'
if param.kind is Parameter.POSITIONAL_ONLY else
'*{0}'
if param.kind is Parameter.VAR_POSITIONAL else
'**{0}'
if param.kind is Parameter.VAR_KEYWORD else
# Intentionally crash for Parameter.POSITIONAL_ONLY.
None).format(param.name)
for param in params) + ')'
MAX_CALL_PREFIX = 18 # len(' __ret = gca().')
Expand Down