Skip to content

Support only positional args in contour. Error if no positional argument. #24749

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 2 commits into from
Jan 27, 2023
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
8 changes: 5 additions & 3 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ def _process_args(self, *args, corner_mask=None, algorithm=None, **kwargs):
"""
Process args and kwargs.
"""
if isinstance(args[0], QuadContourSet):
if args and isinstance(args[0], QuadContourSet):
if self.levels is None:
self.levels = args[0].levels
self.zmin = args[0].zmin
Expand Down Expand Up @@ -1441,13 +1441,15 @@ def _contour_args(self, args, kwargs):
else:
fn = 'contour'
nargs = len(args)
if nargs <= 2:

if 0 < nargs <= 2:
z, *args = args
z = ma.asarray(z)
x, y = self._initialize_x_y(z)
elif nargs <= 4:
elif 2 < nargs <= 4:
x, y, z_orig, *args = args
x, y, z = self._check_xyz(x, y, z_orig, kwargs)

else:
raise _api.nargs_error(fn, takes="from 1 to 4", given=nargs)
z = ma.masked_invalid(z, copy=False)
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,13 @@ def test_contour_remove():
assert ax.get_children() == orig_children


def test_contour_no_args():
fig, ax = plt.subplots()
data = [[0, 1], [1, 0]]
with pytest.raises(TypeError, match=r"contour\(\) takes from 1 to 4"):
ax.contour(Z=data)


def test_bool_autolevel():
x, y = np.random.rand(2, 9)
z = (np.arange(9) % 2).reshape((3, 3)).astype(bool)
Expand Down