diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f1e1116a5a58..8c7a9149f804 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2716,9 +2716,12 @@ def grid(self, b=None, which='major', axis='both', **kwargs): elif b is not None: b = _string_to_bool(b) - if axis == 'x' or axis == 'both': + if axis not in ['x', 'y', 'both']: + raise ValueError("The argument 'axis' must be one of 'x', 'y' or " + "'both'.") + if axis in ['x', 'both']: self.xaxis.grid(b, which=which, **kwargs) - if axis == 'y' or axis == 'both': + if axis in ['y', 'both']: self.yaxis.grid(b, which=which, **kwargs) def ticklabel_format(self, *, axis='both', style='', scilimits=None, diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index fdebfb2156ca..386185bb9efe 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1438,9 +1438,17 @@ def grid(self, b=None, which='major', **kwargs): """ if len(kwargs): + if not b and b is not None: # something false-like but not None + warnings.warn('First parameter to grid() is false, but line ' + 'properties are supplied. The grid will be ' + 'enabled.') b = True which = which.lower() + if which not in ['major', 'minor', 'both']: + raise ValueError("The argument 'which' must be one of 'major', " + "'minor' or 'both'.") gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()} + if which in ['minor', 'both']: if b is None: self._gridOnMinor = not self._gridOnMinor