8000 Merge pull request #20172 from meeseeksmachine/auto-backport-of-pr-20… · matplotlib/matplotlib@f498fdd · GitHub
[go: up one dir, main page]

Skip to content

Commit f498fdd

Browse files
authored
Merge pull request #20172 from meeseeksmachine/auto-backport-of-pr-20161-on-v3.4.x
Backport PR #20161 on branch v3.4.x (Fix resetting grid visibility)
2 parents eab5bed + 93aabc1 commit f498fdd

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
@@ -758,6 +758,18 @@ def get_children(self):
758758
return [self.label, self.offsetText,
759759
*self.get_major_ticks(), *self.get_minor_ticks()]
760760

761+
def _reset_major_tick_kw(self):
762+
self._major_tick_kw.clear()
763+
self._major_tick_kw['gridOn'] = (
764+
mpl.rcParams['axes.grid'] and
765+
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
766+
767+
def _reset_minor_tick_kw(self):
768+
self._minor_tick_kw.clear()
769+
self._minor_tick_kw['gridOn'] = (
770+
mpl.rcParams['axes.grid'] and
771+
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
772+
761773
def clear(self):
762774
"""
763775
Clear the axis.
@@ -779,14 +791,8 @@ def clear(self):
779791
# Clear the callback registry for this axis, or it may "leak"
780792
self.callbacks = cbook.CallbackRegistry()
781793

782-
# whether the grids are on
783-
self._major_tick_kw['gridOn'] = (
784-
mpl.rcParams['axes.grid'] and
785-
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
786-
self._minor_tick_kw['gridOn'] = (
787-
mpl.rcParams['axes.grid'] and
788-
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
789-
794+
self._reset_major_tick_kw()
795+
self._reset_minor_tick_kw()
790796
self.reset_ticks()
791797

792798
self.converter = None
@@ -833,10 +839,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
833839
# future new ticks will automatically get them
834840
if reset:
835841
if which in ['major', 'both']:
836-
self._major_tick_kw.clear()
842+
self._reset_major_tick_kw()
837843
self._major_tick_kw.update(kwtrans)
838844
if which in ['minor', 'both']:
839-
self._minor_tick_kw.clear()
845+
self._reset_minor_tick_kw()
840846
self._minor_tick_kw.update(kwtrans)
841847
self.reset_ticks()
842848
else:
@@ -1385,35 +1391,40 @@ def grid(self, b=None, which='major', **kwargs):
13851391
13861392
grid(color='r', linestyle='-', linewidth=2)
13871393
"""
1388-
if b is not None:
1389-
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
1394+
TOGGLE = object()
1395+
UNSET = object()
1396+
visible = kwargs.pop('visible', UNSET)
1397+
1398+
if b is None:
1399+
if visible is UNSET:
1400+
if kwargs: # grid(color='r')
1401+
b = True
1402+
else: # grid()
1403+
b = TOGGLE
1404+
else: # grid(visible=v)
1405+
b = visible
1406+
else:
1407+
if visible is not UNSET and bool(b) != bool(visible):
1408+
# grid(True, visible=False), grid(False, visible=True)
13901409
raise ValueError(
13911410
"'b' and 'visible' specify inconsistent grid visibilities")
13921411
if kwargs and not b: # something false-like but not None
1412+
# grid(0, visible=True)
13931413
_api.warn_external('First parameter to grid() is false, '
13941414
'but line properties are supplied. The '
13951415
'grid will be enabled.')
13961416
b = True
1417+
13971418
which = which.lower()
13981419
_api.check_in_list(['major', 'minor', 'both'], which=which)
13991420
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1400-
if 'grid_visible' in gridkw:
1401-
forced_visibility = True
1402-
gridkw['gridOn'] = gridkw.pop('grid_visible')
1403-
else:
1404-
forced_visibility = False
1405-
14061421
if which in ['minor', 'both']:
1407-
if b is None and not forced_visibility:
1408-
gridkw['gridOn'] = not self._minor_tick_kw['gridOn']
1409-
elif b is not None:
1410-
gridkw['gridOn'] = b
1422+
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
1423+
if b is TOGGLE else b)
14111424
self.set_tick_params(which< 23D3 span class=pl-c1>='minor', **gridkw)
14121425
if which in ['major', 'both']:
1413-
if b is None and not forced_visibility:
1414-
gridkw['gridOn'] = not self._major_tick_kw['gridOn']
1415-
elif b is not None:
1416-
gridkw['gridOn'] = b
1426+
gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
1427+
if b is TOGGLE else b)
14171428
self.set_tick_params(which='major', **gridkw)
14181429
self.stale = True
14191430

lib/matplotlib/tests/test_axes.py

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

47394739

4740+
def test_reset_grid():
4741+
fig, ax = plt.subplots()
4742+
ax.tick_params(reset=True, which='major', labelsize=10)
4743+
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
4744+
ax.grid(color='red') # enables grid
4745+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4746+
4747+
with plt.rc_context({'axes.grid': True}):
4748+
ax.clear()
4749+
ax.tick_params(reset=True, which='major', labelsize=10)
4750+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4751+
4752+
47404753
def test_vline_limit():
47414754
fig = plt.figure()
47424755
ax = fig.gca()

0 commit comments

Comments
 (0)
0