Skip to content

Add new "reset" property to clear (cla) method #28974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
40 changes: 29 additions & 11 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@
mpl.rcParams['axes.grid'] and
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))

def clear(self):
def clear(self, **kwargs):
"""
Clear the axis.

Expand All @@ -860,17 +860,35 @@
- 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(

Check warning on line 871 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L871

Added line #L871 was not covered by tests
"Method 'clear' can accept only one argument")
if 'reset' in kwargs:
reset = kwargs.pop('reset')

Check warning on line 874 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L874

Added line #L874 was not covered by tests
else:
reset = False # Default
if type(reset) is not bool:
reset = False # Default
_log.debug(

Check warning on line 879 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L878-L879

Added lines #L878 - L879 were not covered by tests
"kwarg 'reset' must be a bool. It will be set to False")
if reset:
self.label._reset_visual_defaults()

Check warning on line 882 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L882

Added line #L882 was not covered by tests
# 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']

Check warning on line 889 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L885-L889

Added lines #L885 - L889 were not covered by tests

self._init()

Check warning on line 891 in lib/matplotlib/axis.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axis.py#L891

Added line #L891 was not covered by tests

self._set_scale('linear')

Expand Down
Loading