Skip to content

Commit 12c6a0a

Browse files
committed
Argument checking for grid()
1 parent c552bdc commit 12c6a0a

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,9 +2716,12 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
27162716
elif b is not None:
27172717
b = _string_to_bool(b)
27182718

2719-
if axis == 'x' or axis == 'both':
2719+
if axis not in ['x', 'y', 'both']:
2720+
raise ValueError("The argument 'axis' must be one of 'x', 'y' or "
2721+
"'both'.")
2722+
if axis in ['x', 'both']:
27202723
self.xaxis.grid(b, which=which, **kwargs)
2721-
if axis == 'y' or axis == 'both':
2724+
if axis in ['y', 'both']:
27222725
self.yaxis.grid(b, which=which, **kwargs)
27232726

27242727
def ticklabel_format(self, *, axis='both', style='', scilimits=None,

lib/matplotlib/axis.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,9 +1438,17 @@ def grid(self, b=None, which='major', **kwargs):
14381438
14391439
"""
14401440
if len(kwargs):
1441+
if not b and b is not None: # something false-like but not None
1442+
warnings.warn('First parameter to grid() is false, but line '
1443+
'properties are supplied. The grid will be '
1444+
'enabled.')
14411445
b = True
14421446
which = which.lower()
1447+
if which not in ['major', 'minor', 'both']:
1448+
raise ValueError("The argument 'which' must be one of 'major', "
1449+
"'minor' or 'both'.")
14431450
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1451+
14441452
if which in ['minor', 'both']:
14451453
if b is None:
14461454
self._gridOnMinor = not self._gridOnMinor

0 commit comments

Comments
 (0)