8000 Merge pull request #22587 from timhoffm/refactor-tick-clearing · matplotlib/matplotlib@945692c · GitHub
[go: up one dir, main page]

Skip to content

Commit 945692c

Browse files
authored
Merge pull request #22587 from timhoffm/refactor-tick-clearing
Refactor handling of tick and ticklabel visiblity in Axis.clear()
2 parents 223b2b1 + 2357c92 commit 945692c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

lib/matplotlib/axis.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -797,14 +797,36 @@ def get_children(self):
797797
return [self.label, self.offsetText,
798798
*self.get_major_ticks(), *self.get_minor_ticks()]
799799

800-
def _reset_major_tick_kw(self):
800+
def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False):
801+
"""
802+
Reset major tick params to defaults.
803+
804+
Shared subplots pre-configure tick and label visibility. To keep this
805+
beyond an Axis.clear() operation, we may
806+
*keep_tick_and_label_visibility*.
807+
"""
808+
backup = {name: value for name, value in self._major_tick_kw.items()
809+
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
801810
self._major_tick_kw.clear()
811+
if keep_tick_and_label_visibility:
812+
self._major_tick_kw.update(backup)
802813
self._major_tick_kw['gridOn'] = (
803814
mpl.rcParams['axes.grid'] and
804815
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
805816

806-
def _reset_minor_tick_kw(self):
817+
def _reset_minor_tick_kw(self, keep_tick_and_label_visibility=False):
818+
"""
819+
Reset minor tick params to defaults.
820+
821+
Shared subplots pre-configure tick and label visibility. To keep this
822+
beyond an Axis.clear() operation, we may
823+
*keep_tick_and_label_visibility*.
824+
"""
825+
backup = {name: value for name, value in self._minor_tick_kw.items()
826+
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
807827
self._minor_tick_kw.clear()
828+
if keep_tick_and_label_visibility:
829+
self._minor_tick_kw.update(backup)
808830
self._minor_tick_kw['gridOn'] = (
809831
mpl.rcParams['axes.grid'] and
810832
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
@@ -821,6 +843,8 @@ def clear(self):
821843
- major and minor grid
822844
- units
823845
- registered callbacks
846+
847+
This does not reset tick and tick label visibility.
824848
"""
825849

826850
self.label.set_text('') # self.set_label_text would change isDefault_
@@ -832,12 +856,8 @@ def clear(self):
832856
signals=["units", "units finalize"])
833857

834858
# whether the grids are on
835-
self._major_tick_kw['gridOn'] = (
836-
mpl.rcParams['axes.grid'] and
837-
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
838-
self._minor_tick_kw['gridOn'] = (
839-
mpl.rcParams['axes.grid'] and
840-
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
859+
self._reset_major_tick_kw(keep_tick_and_label_visibility=True)
860+
self._reset_minor_tick_kw(keep_tick_and_label_visibility=True)
841861
self.reset_ticks()
842862

843863
self.converter = None

0 commit comments

Comments
 (0)
0