diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 62dee0983fdb..278dab735e6f 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2885,8 +2885,6 @@ def grid(self, b=None, which='major', axis='both', **kwargs): use `.set_axisbelow` or, for more control, call the `~.Artist.set_zorder` method of each axis. """ - if len(kwargs): - b = True cbook._check_in_list(['x', 'y', 'both'], axis=axis) if axis in ['x', 'both']: self.xaxis.grid(b, which=which, **kwargs) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index f970a4452660..10d132f03694 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -778,10 +778,10 @@ def cla(self): self.callbacks = cbook.CallbackRegistry() # whether the grids are on - self._gridOnMajor = ( + self._major_tick_kw['gridOn'] = ( mpl.rcParams['axes.grid'] and mpl.rcParams['axes.grid.which'] in ('both', 'major')) - self._gridOnMinor = ( + self._minor_tick_kw['gridOn'] = ( mpl.rcParams['axes.grid'] and mpl.rcParams['axes.grid.which'] in ('both', 'minor')) @@ -1381,7 +1381,6 @@ def get_major_ticks(self, numticks=None): # Update the new tick label properties from the old. tick = self._get_tick(major=True) self.majorTicks.append(tick) - tick.gridline.set_visible(self._gridOnMajor) self._copy_tick_props(self.majorTicks[0], tick) return self.majorTicks[:numticks] @@ -1395,7 +1394,6 @@ def get_minor_ticks(self, numticks=None): # Update the new tick label properties from the old. tick = self._get_tick(major=False) self.minorTicks.append(tick) - tick.gridline.set_visible(self._gridOnMinor) self._copy_tick_props(self.minorTicks[0], tick) return self.minorTicks[:numticks] @@ -1420,32 +1418,37 @@ def grid(self, b=None, which='major', **kwargs): Define the line properties of the grid, e.g.:: grid(color='r', linestyle='-', linewidth=2) - """ - if len(kwargs): - if not b and b is not None: # something false-like but not None + if b is not None: + if 'visible' in kwargs and bool(b) != bool(kwargs['visible']): + raise ValueError( + "'b' and 'visible' specify inconsistent grid visibilities") + if kwargs and not b: # something false-like but not None cbook._warn_external('First parameter to grid() is false, ' 'but line properties are supplied. The ' 'grid will be enabled.') - b = True + b = True which = which.lower() cbook._check_in_list(['major', 'minor', 'both'], which=which) gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()} + if 'grid_visible' in gridkw: + forced_visibility = True + gridkw['gridOn'] = gridkw.pop('grid_visible') + else: + forced_visibility = False if which in ['minor', 'both']: - if b is None: - self._gridOnMinor = not self._gridOnMinor - else: - self._gridOnMinor = b - self.set_tick_params(which='minor', gridOn=self._gridOnMinor, - **gridkw) + if b is None and not forced_visibility: + gridkw['gridOn'] = not self._minor_tick_kw['gridOn'] + elif b is not None: + gridkw['gridOn'] = b + self.set_tick_params(which='minor', **gridkw) if which in ['major', 'both']: - if b is None: - self._gridOnMajor = not self._gridOnMajor - else: - self._gridOnMajor = b - self.set_tick_params(which='major', gridOn=self._gridOnMajor, - **gridkw) + if b is None and not forced_visibility: + gridkw['gridOn'] = not self._major_tick_kw['gridOn'] + elif b is not None: + gridkw['gridOn'] = b + self.set_tick_params(which='major', **gridkw) self.stale = True def update_units(self, data): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b31ebc945fac..57beec025aa8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4310,26 +4310,35 @@ def test_twin_spines_on_top(): ax2.fill_between("i", "j", color='#7FC97F', alpha=.5, data=data) -def test_rcparam_grid_minor(): - orig_grid = matplotlib.rcParams['axes.grid'] - orig_locator = matplotlib.rcParams['axes.grid.which'] - - matplotlib.rcParams['axes.grid'] = True - - values = ( - (('both'), (True, True)), - (('major'), (True, False)), - (('minor'), (False, True)) - ) +@pytest.mark.parametrize("grid_which, major_visible, minor_visible", [ + ("both", True, True), + ("major", True, False), + ("minor", False, True), +]) +def test_rcparam_grid_minor(grid_which, major_visible, minor_visible): + mpl.rcParams.update({"axes.grid": True, "axes.grid.which": grid_which}) + fig, ax = plt.subplots() + fig.canvas.draw() + assert all(tick.gridline.get_visible() == major_visible + for tick in ax.xaxis.majorTicks) + assert all(tick.gridline.get_visible() == minor_visible + for tick in ax.xaxis.minorTicks) - for locator, result in values: - matplotlib.rcParams['axes.grid.which'] = locator - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) - assert (ax.xaxis._gridOnMajor, ax.xaxis._gridOnMinor) == result - matplotlib.rcParams['axes.grid'] = orig_grid - matplotlib.rcParams['axes.grid.which'] = orig_locator +def test_grid(): + fig, ax = plt.subplots() + ax.grid() + fig.canvas.draw() + assert ax.xaxis.majorTicks[0].gridline.get_visible() + ax.grid(visible=False) + fig.canvas.draw() + assert not ax.xaxis.majorTicks[0].gridline.get_visible() + ax.grid(visible=True) + fig.canvas.draw() + assert ax.xaxis.majorTicks[0].gridline.get_visible() + ax.grid() + fig.canvas.draw() + assert not ax.xaxis.majorTicks[0].gridline.get_visible() def test_vline_limit(): diff --git a/lib/mpl_toolkits/axisartist/axislines.py b/lib/mpl_toolkits/axisartist/axislines.py index db89bd434eb8..d3e32477adf2 100644 --- a/lib/mpl_toolkits/axisartist/axislines.py +++ b/lib/mpl_toolkits/axisartist/axislines.py @@ -439,9 +439,9 @@ def get_gridlines(self, which="major", axis="both"): if axis in ["both", "y"]: x1, x2 = self.axes.get_xlim() locs = [] - if self.axes.yaxis._gridOnMajor: + if self.axes.yaxis._major_tick_kw["gridOn"]: locs.extend(self.axes.yaxis.major.locator()) - if self.axes.yaxis._gridOnMinor: + if self.axes.yaxis._minor_tick_kw["gridOn"]: locs.extend(self.axes.yaxis.minor.locator()) for y in locs: @@ -533,17 +533,17 @@ def grid(self, b=None, which='major', axis="both", **kwargs): """ Toggle the gridlines, and optionally set the properties of the lines. """ - # their are some discrepancy between the behavior of grid in - # axes_grid and the original mpl's grid, because axes_grid - # explicitly set the visibility of the gridlines. + # There are some discrepancies in the behavior of grid() between + # axes_grid and Matplotlib, because axes_grid explicitly sets the + # visibility of the gridlines. super().grid(b, which=which, axis=axis, **kwargs) if not self._axisline_on: return if b is None: - b = (self.axes.xaxis._gridOnMinor - or self.axes.xaxis._gridOnMajor - or self.axes.yaxis._gridOnMinor - or self.axes.yaxis._gridOnMajor) + b = (self.axes.xaxis._minor_tick_kw["gridOn"] + or self.axes.xaxis._major_tick_kw["gridOn"] + or self.axes.yaxis._minor_tick_kw["gridOn"] + or self.axes.yaxis._major_tick_kw["gridOn"]) self.gridlines.set(which=which, axis=axis, visible=b) self.gridlines.set(**kwargs)