10000 Fix resetting grid visibility · matplotlib/matplotlib@b2fcfc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2fcfc7

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 b2fcfc7

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/matplotlib/axis.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,18 @@ 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.clear()
791+
self._major_tick_kw['gridOn'] = (
792+
mpl.rcParams['axes.grid'] and
793+
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
794+
795+
def _reset_minor_tick_kw(self):
796+
self._minor_tick_kw.clear()
797+
self._minor_tick_kw['gridOn'] = (
798+
mpl.rcParams['axes.grid'] and
799+
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
800+
789801
def clear(self):
790802
"""
791803
Clear the axis.
@@ -807,14 +819,8 @@ def clear(self):
807819
# Clear the callback registry for this axis, or it may "leak"
808820
self.callbacks = cbook.CallbackRegistry()
809821

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-
822+
self._reset_major_tick_kw()
823+
self._reset_minor_tick_kw()
818824
self.reset_ticks()
819825

820826
self.converter = None
@@ -861,10 +867,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
861867
# future new ticks will automatically get them
862868
if reset:
863869
if which in ['major', 'both']:
864-
self._major_tick_kw.clear()
870+
self._reset_major_tick_kw()
865871
self._major_tick_kw.update(kwtrans)
866872
if which in ['minor', 'both']:
867-
self._minor_tick_kw.clear()
873+
self._reset_minor_tick_kw()
868874
self._minor_tick_kw.update(kwtrans)
869875
self.reset_ticks()
870876
else:
@@ -1413,6 +1419,9 @@ def grid(self, b=None, which='major', **kwargs):
14131419
14141420
grid(color='r', linestyle='-', linewidth=2)
14151421
"""
1422+
if b is None and kwargs:
1423+
b = True # if any kwargs are supplied, assume you want the grid on
1424+
14161425
if b is not None:
14171426
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
14181427
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