Skip to content

Backport PR #20161 on branch v3.4.x (Fix resetting grid visibility) #20172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,18 @@ def get_children(self):
return [self.label, self.offsetText,
*self.get_major_ticks(), *self.get_minor_ticks()]

def _reset_major_tick_kw(self):
self._major_tick_kw.clear()
self._major_tick_kw['gridOn'] = (
mpl.rcParams['axes.grid'] and
mpl.rcParams['axes.grid.which'] in ('both', 'major'))

def _reset_minor_tick_kw(self):
self._minor_tick_kw.clear()
self._minor_tick_kw['gridOn'] = (
mpl.rcParams['axes.grid'] and
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))

def clear(self):
"""
Clear the axis.
Expand All @@ -779,14 +791,8 @@ def clear(self):
# Clear the callback registry for this axis, or it may "leak"
self.callbacks = cbook.CallbackRegistry()

# whether the grids are on
self._major_tick_kw['gridOn'] = (
mpl.rcParams['axes.grid'] and
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
self._minor_tick_kw['gridOn'] = (
mpl.rcParams['axes.grid'] and
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))

self._reset_major_tick_kw()
self._reset_minor_tick_kw()
self.reset_ticks()

self.converter = None
Expand Down Expand Up @@ -833,10 +839,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
# future new ticks will automatically get them
if reset:
if which in ['major', 'both']:
self._major_tick_kw.clear()
self._reset_major_tick_kw()
self._major_tick_kw.update(kwtrans)
if which in ['minor', 'both']:
self._minor_tick_kw.clear()
self._reset_minor_tick_kw()
self._minor_tick_kw.update(kwtrans)
self.reset_ticks()
else:
Expand Down Expand Up @@ -1385,35 +1391,40 @@ def grid(self, b=None, which='major', **kwargs):

grid(color='r', linestyle='-', linewidth=2)
"""
if b is not None:
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
TOGGLE = object()
UNSET = object()
visible = kwargs.pop('visible', UNSET)

if b is None:
if visible is UNSET:
if kwargs: # grid(color='r')
b = True
else: # grid()
b = TOGGLE
else: # grid(visible=v)
b = visible
else:
if visible is not UNSET and bool(b) != bool(visible):
# grid(True, visible=False), grid(False, visible=True)
raise ValueError(
"'b' and 'visible' specify inconsistent grid visibilities")
if kwargs and not b: # something false-like but not None
# grid(0, visible=True)
_api.warn_external('First parameter to grid() is false, '
'but line properties are supplied. The '
'grid will be enabled.')
b = True

which = which.lower()
_api.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 and not forced_visibility:
gridkw['gridOn'] = not self._minor_tick_kw['gridOn']
elif b is not None:
gridkw['gridOn'] = b
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
if b is TOGGLE else b)
self.set_tick_params(which='minor', **gridkw)
if which in ['major', 'both']:
if b is None and not forced_visibility:
gridkw['gridOn'] = not self._major_tick_kw['gridOn']
elif b is not None:
gridkw['gridOn'] = b
gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
if b is TOGGLE else b)
self.set_tick_params(which='major', **gridkw)
self.stale = True

Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4737,6 +4737,19 @@ def test_grid():
assert not ax.xaxis.majorTicks[0].gridline.get_visible()


def test_reset_grid():
fig, ax = plt.subplots()
ax.tick_params(reset=True, which='major', labelsize=10)
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
ax.grid(color='red') # enables grid
assert ax.xaxis.majorTicks[0].gridline.get_visible()

with plt.rc_context({'axes.grid': True}):
ax.clear()
ax.tick_params(reset=True, which='major', labelsize=10)
assert ax.xaxis.majorTicks[0].gridline.get_visible()


def test_vline_limit():
fig = plt.figure()
ax = fig.gca()
Expand Down