8000 Merge pull request #20161 from timhoffm/grid-reset · matplotlib/matplotlib@2b8590c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b8590c

Browse files
authored
Merge pull request #20161 from timhoffm/grid-reset
Fix resetting grid visibility
2 parents 40862a3 + a15bc47 commit 2b8590c

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

lib/matplotlib/axis.py

Lines changed: 37 additions & 26 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,35 +1419,40 @@ def grid(self, b=None, which='major', **kwargs):
14131419
14141420
grid(color='r', linestyle='-', linewidth=2)
14151421
"""
1416-
if b is not None:
1417-
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
1422+
TOGGLE = object()
1423+
UNSET = object()
1424+
visible = kwargs.pop('visible', UNSET)
1425+
1426+
if b is None:
1427+
if visible is UNSET:
1428+
if kwargs: # grid(color='r')
1429+
b = True
1430+
else: # grid()
1431+
b = TOGGLE
1432+
else: # grid(visible=v)
1433+
b = visible
1434+
else:
1435+
if visible is not UNSET and bool(b) != bool(visible):
1436+
# grid(True, visible=False), grid(False, visible=True)
14181437
raise ValueError(
14191438
"'b' and 'visible' specify inconsistent grid visibilities")
14201439
if kwargs and not b: # something false-like but not None
1440+
# grid(0, visible=True)
14211441
_api.warn_external('First parameter to grid() is false, '
14221442
'but line properties are supplied. The '
14231443
'grid will be enabled.')
14241444
b = True
1445+
14251446
which = which.lower()
14261447
_api.check_in_list(['major', 'minor', 'both'], which=which)
14271448
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1428-
if 'grid_visible' in gridkw:
1429-
forced_visibility = True
1430-
gridkw['gridOn'] = gridkw.pop('grid_visible')
1431-
else:
1432-
forced_visibility = False
1433-
14341449
if which in ['minor', 'both']:
1435-
if b is None and not forced_visibility:
1436-
gridkw['gridOn'] = not self._minor_tick_kw['gridOn']
1437-
elif b is not None:
1438-
gridkw['gridOn'] = b
1450+
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
1451+
if b is TOGGLE else b)
14391452
self.set_tick_params(which='minor', **gridkw)
14401453
if which in ['major', 'both']:
1441-
if b is None and not forced_visibility:
1442-
gridkw['gridOn'] = not self._major_tick_kw['gridOn']
1443-
elif b is not None:
1444-
gridkw['gridOn'] = b
1454+
gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
1455+
if b is TOGGLE else b)
14451456
self.set_tick_params(which='major', **gridkw)
14461457
self.stale = True
14471458

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