diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 42096958bb93..6e2190e8bd8d 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -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 @@ -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) diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 41d4dc8501bd..58101a9adc24 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -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)