Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e7a5c3

Browse files
authored
Merge pull request #11784 from timhoffm/axes-grid
Argument checking for grid()
2 parents 10a981a + 12c6a0a commit 0e7a5c3

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
@@ -2715,9 +2715,12 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
27152715
elif b is not None:
27162716
b = _string_to_bool(b)
27172717

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

27232726
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)