8000 Fix resetting grid visibility · matplotlib/matplotlib@3e4a204 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e4a204

Browse files
committed
Fix resetting grid visibility
Actually, there were two issues: - 'gridOn' state should always be contained in the _tick_kw settings. This is needed because we support toggling via grid() and thus need to be able to query the state. Therefore, the _tick_kw dicts must be resetted instead of cleared. - grid(some_kwarg=...) i.e. with b=None and keywords must be treated explicitly as b=True as the docs already propose. Closes #20149.
1 parent 363105b commit 3e4a204

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

lib/matplotlib/axis.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,16 @@ def get_children(self):
786786
return [self.label, self.offsetText,
787787
*self.get_major_ticks(), *self.get_minor_ticks()]
788788

789+
def _reset_major_tick_kw(self):
790+
self._major_tick_kw['gridOn'] = (
791+
mpl.rcParams['axes.grid'] and
792+
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
793+
794+
def _reset_minor_tick_kw(self):
795+
self._minor_tick_kw['gridOn'] = (
796+
mpl.rcParams['axes.grid'] and
797+
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
798+
789799
def clear(self):
790800
"""
791801
Clear the axis.
@@ -807,14 +817,8 @@ def clear(self):
807817
# Clear the callback registry for this axis, or it may "leak"
808818
self.callbacks = cbook.CallbackRegistry()
809819

810-
# whether the grids are on
811-
self._major_tick_kw['gridOn'] = (
812-
mpl.rcParams['axes.grid'] and
813-
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
814-
self._minor_tick_kw['gridOn'] = (
815-
mpl.rcParams['axes.grid'] and
816-
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
817-
820+
self._reset_major_tick_kw()
821+
self._reset_minor_tick_kw()
818822
self.reset_ticks()
819823

820824
self.converter = None
@@ -861,10 +865,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
861865
# future new ticks will automatically get them
862866
if reset:
863867
if which in ['major', 'both']:
864-
self._major_tick_kw.clear()
868+
self._reset_major_tick_kw()
865869
self._major_tick_kw.update(kwtrans)
866870
if which in ['minor', 'both']:
867-
self._minor_tick_kw.clear()
871+
self._reset_minor_tick_kw()
868872
self._minor_tick_kw.update(kwtrans)
869873
self.reset_ticks()
870874
else:
@@ -1413,6 +1417,9 @@ def grid(self, b=None, which='major', **kwargs):
14131417
14141418
grid(color='r', linestyle='-', linewidth=2)
14151419
"""
1420+
if b is None and kwargs:
1421+
b = True # if any kwargs are supplied, assume you want the grid on
1422+
14161423
if b is not None:
14171424
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
14181425
raise ValueError(

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,6 +4757,19 @@ def test_grid():
47574757
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
47584758

47594759

4760+
def test_reset_grid():
4761+
fig, ax = plt.subplots()
4762+
ax.tick_params(reset=True, which='major', labelsize=10)
4763+
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
4764+
ax.grid(color='red') # enables grid
4765+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4766+
4767+
with plt.rc_context({'axes.grid': True}):
4768+
ax.clear()
4769+
ax.tick_params(reset=True, which='major', labelsize=10)
4770+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4771+
4772+
47604773
def test_vline_limit():
47614774
fig = plt.figure()
47624775
ax = fig.gca()

0 commit comments

Comments
 (0)
0