From cf9d1d9c53756f6a653b5d7641bb6db72db97216 Mon Sep 17 00:00:00 2001 From: daniele99 Date: Sat, 12 Oct 2024 18:38:30 +0200 Subject: [PATCH] Add new reset property to clear (cla) method --- lib/matplotlib/axes/_base.py | 16 +++++++-------- lib/matplotlib/axis.py | 40 ++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8fc9e3e3cf4d..9ddbbee66251 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1262,7 +1262,7 @@ def sharey(self, other): self.set_ylim(y0, y1, emit=False, auto=other.get_autoscaley_on()) self.yaxis._scale = other.yaxis._scale - def __clear(self): + def __clear(self, **kwargs): """Clear the Axes.""" # The actual implementation of clear() as long as clear() has to be # an adapter delegating to the correct implementation. @@ -1279,7 +1279,7 @@ def __clear(self): yaxis_visible = self.yaxis.get_visible() for axis in self._axis_map.values(): - axis.clear() # Also resets the scale to linear. + axis.clear(**kwargs) # Also resets the scale to linear. for spine in self.spines.values(): spine._clear() # Use _clear to not clear Axis again @@ -1389,23 +1389,23 @@ def __clear(self): self.stale = True - def clear(self): + def clear(self, **kwargs): """Clear the Axes.""" # Act as an alias, or as the superclass implementation depending on the # subclass implementation. if self._subclass_uses_cla: - self.cla() + self.cla(**kwargs) else: - self.__clear() + self.__clear(**kwargs) - def cla(self): + def cla(self, **kwargs): """Clear the Axes.""" # Act as an alias, or as the superclass implementation depending on the # subclass implementation. if self._subclass_uses_cla: - self.__clear() + self.__clear(**kwargs) else: - self.clear() + self.clear(**kwargs) class ArtistList(Sequence): """ diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 8e612bd8c702..e658a86bb1d5 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -848,7 +848,7 @@ def _reset_minor_tick_kw(self): mpl.rcParams['axes.grid'] and mpl.rcParams['axes.grid.which'] in ('both', 'minor')) - def clear(self): + def clear(self, **kwargs): """ Clear the axis. @@ -860,17 +860,35 @@ def clear(self): - major and minor grid - units - registered callbacks - """ - self.label._reset_visual_defaults() - # The above resets the label formatting using text rcParams, - # so we then update the formatting using axes rcParams - self.label.set_color(mpl.rcParams['axes.labelcolor']) - self.label.set_fontsize(mpl.rcParams['axes.labelsize']) - self.label.set_fontweight(mpl.rcParams['axes.labelweight']) - self.offsetText._reset_visual_defaults() - self.labelpad = mpl.rcParams['axes.labelpad'] - self._init() + If no *kwargs* are passed *'reset'* is set by default to *False*. + + When *'reset'* is set to *False*, part of the formatting of the axis + (labels and relative properties) is left unchanged. Thus, a "soft" + clear is executed in this case. + """ + if len(kwargs) > 1: + _log.debug( + "Method 'clear' can accept only one argument") + if 'reset' in kwargs: + reset = kwargs.pop('reset') + else: + reset = False # Default + if type(reset) is not bool: + reset = False # Default + _log.debug( + "kwarg 'reset' must be a bool. It will be set to False") + if reset: + self.label._reset_visual_defaults() + # The above resets the label formatting using text rcParams, + # so we then update the formatting using axes rcParams + self.label.set_color(mpl.rcParams['axes.labelcolor']) + self.label.set_fontsize(mpl.rcParams['axes.labelsize']) + self.label.set_fontweight(mpl.rcParams['axes.labelweight']) + self.offsetText._reset_visual_defaults() + self.labelpad = mpl.rcParams['axes.labelpad'] + + self._init() self._set_scale('linear')