Skip to content

Commit 2357c92

Browse files
committed
Refactor handling of tick and ticklabel visiblity in Axis.clear()
This is a follow-up to #20826, which makes the exceptions from clearing more explicit.
1 parent 2bb9b03 commit 2357c92

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

lib/matplotlib/axis.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,36 @@ def get_children(self):
781781
return [self.label, self.offsetText,
782782
*self.get_major_ticks(), *self.get_minor_ticks()]
783783

784-
def _reset_major_tick_kw(self):
784+
def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False):
785+
"""
786+
Reset major tick params to defaults.
787+
788+
Shared subplots pre-configure tick and label visibility. To keep this
789+
beyond an Axis.clear() operation, we may
790+
*keep_tick_and_label_visibility*.
791+
"""
792+
backup = {name: value for name, value in self._major_tick_kw.items()
793+
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
785794
self._major_tick_kw.clear()
795+
if keep_tick_and_label_visibility:
796+
self._major_tick_kw.update(backup)
786797
self._major_tick_kw['gridOn'] = (
787798
mpl.rcParams['axes.grid'] and
788799
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
789800

790-
def _reset_minor_tick_kw(self):
801+
def _reset_minor_tick_kw(self, keep_tick_and_label_visibility=False):
802+
"""
803+
Reset minor tick params to defaults.
804+
805+
Shared subplots pre-configure tick and label visibility. To keep this
806+
beyond an Axis.clear() operation, we may
807+
*keep_tick_and_label_visibility*.
808+
"""
809+
backup = {name: value for name, value in self._minor_tick_kw.items()
810+
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
791811
self._minor_tick_kw.clear()
812+
if keep_tick_and_label_visibility:
813+
self._minor_tick_kw.update(backup)
792814
self._minor_tick_kw['gridOn'] = (
793815
mpl.rcParams['axes.grid'] and
794816
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
@@ -805,6 +827,8 @@ def clear(self):
805827
- major and minor grid
806828
- units
807829
- registered callbacks
830+
831+
This does not reset tick and tick label visibility.
808832
"""
809833

810834
self.label.set_text('') # self.set_label_text would change isDefault_
@@ -816,12 +840,8 @@ def clear(self):
816840
signals=["units", "units finalize"])
817841

818842
# whether the grids are on
819-
self._major_tick_kw['gridOn'] = (
820-
mpl.rcParams['axes.grid'] and
821-
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
822-
self._minor_tick_kw['gridOn'] = (
823-
mpl.rcParams['axes.grid'] and
824-
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
843+
self._reset_major_tick_kw(keep_tick_and_label_visibility=True)
844+
self._reset_minor_tick_kw(keep_tick_and_label_visibility=True)
825845
self.reset_ticks()
826846

827847
self.converter = None

0 commit comments

Comments
 (0)