Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b7c237

Browse files
committed
MNT: prefer "clear" as canonical over clf
1 parent a395083 commit 6b7c237

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

lib/matplotlib/figure.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def _break_share_link(ax, grouper):
913913
# Break link between any twinned axes
914914
_break_share_link(ax, ax._twinned_axes)
915915

916-
def clf(self, keep_observers=False):
916+
def clear(self, keep_observers=False):
917917
"""
918918
Clear the figure.
919919
@@ -928,7 +928,7 @@ def clf(self, keep_observers=False):
928928

929929
# first clear the axes in any subfigures
930930
for subfig in self.subfigs:
931-
subfig.clf(keep_observers=keep_observers)
931+
subfig.clear(keep_observers=keep_observers)
932932
self.subfigs = []
933933

934934
for ax in tuple(self.axes): # Iterate over the copy.
@@ -949,13 +949,22 @@ def clf(self, keep_observers=False):
949949

950950
self.stale = True
951951

952-
# synonym for `clf`."""
953-
clear = clf
952+
# synonym for `clear`.
953+
def clf(self, keep_observers=False):
954+
"""
955+
Alias for the `clear()` method.
956+
957+
Parameters
958+
----------
959+
keep_observers: bool, default: False
960+
Set *keep_observers* to True if, for example,
961+
a gui widget is tracking the Axes in the figure.
962+
"""
963+
return self.clear(keep_observers=keep_observers)
954964

955965
# Note: in the docstring below, the newlines in the examples after the
956966
# calls to legend() allow replacing it with figlegend() to generate the
957967
# docstring of pyplot.figlegend.
958-
959968
@_docstring.dedent_interpd
960969
def legend(self, *args, **kwargs):
961970
"""
@@ -2340,7 +2349,7 @@ def __init__(self,
23402349
self.subplotpars = subplotpars
23412350

23422351
self._axstack = _AxesStack() # track all figure axes and current axes
2343-
self.clf()
2352+
self.clear()
23442353
self._cachedRenderer = None
23452354

23462355
# list of child gridspecs for this figure
@@ -2839,10 +2848,10 @@ def set_figheight(self, val, forward=True):
28392848
"""
28402849
self.set_size_inches(self.get_figwidth(), val, forward=forward)
28412850

2842-
def clf(self, keep_observers=False):
2851+
def clear(self, keep_observers=False):
28432852
# docstring inherited
2844-
super().clf(keep_observers=keep_observers)
2845-
# FigureBase.clf does not clear toolbars, as
2853+
super().clear(keep_observers=keep_observers)
2854+
# FigureBase.clear does not clear toolbars, as
28462855
# only Figure can have toolbars
28472856
toolbar = self.canvas.toolbar
28482857
if toolbar is not None:

lib/matplotlib/tests/test_figure.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
1717
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1818
from matplotlib.axes import Axes
19-
from matplotlib.figure import Figure
19+
from matplotlib.figure import Figure, FigureBase
2020
from matplotlib.layout_engine import (ConstrainedLayoutEngine,
2121
TightLayoutEngine)
2222
from matplotlib.ticker import AutoMinorLocator, FixedFormatter, ScalarFormatter
@@ -709,7 +709,8 @@ def test_removed_axis():
709709
fig.canvas.draw()
710710

711711

712-
def test_figure_clear():
712+
@pytest.mark.parametrize('clear_meth', ['clear', 'clf'])
713+
def test_figure_clear(clear_meth):
713714
# we test the following figure clearing scenarios:
714715
fig = plt.figure()
715716

@@ -719,19 +720,19 @@ def test_figure_clear():
719720

720721
# b) a figure with a single unnested axes
721722
ax = fig.add_subplot(111)
722-
fig.clear()
723+
getattr(fig, clear_meth)()
723724
assert fig.axes == []
724725

725726
# c) a figure multiple unnested axes
726727
axes = [fig.add_subplot(2, 1, i+1) for i in range(2)]
727-
fig.clear()
728+
getattr(fig, clear_meth)()
728729
assert fig.axes == []
729730

730731
# d) a figure with a subfigure
731732
gs = fig.add_gridspec(ncols=2, nrows=1)
732733
subfig = fig.add_subfigure(gs[0])
733734
subaxes = subfig.add_subplot(111)
734-
fig.clear()
735+
getattr(fig, clear_meth)()
735736
assert subfig not in fig.subfigs
736737
assert fig.axes == []
737738

@@ -755,15 +756,15 @@ def test_figure_clear():
755756
subaxes = subfig.add_subplot(111)
756757
assert mainaxes in fig.axes
757758
assert subaxes in fig.axes
758-
subfig.clear()
759+
getattr(subfig, clear_meth)()
759760
assert subfig in fig.subfigs
760761
assert subaxes not in subfig.axes
761762
assert subaxes not in fig.axes
762763
assert mainaxes in fig.axes
763764

764765
# e.4) clearing the whole thing
765766
subaxes = subfig.add_subplot(111)
766-
fig.clear()
767+
getattr(fig, clear_meth)()
767768
assert fig.axes == []
768769
assert fig.subfigs == []
769770

@@ -774,22 +775,28 @@ def test_figure_clear():
774775
assert all(sfig in fig.subfigs for sfig in subfigs)
775776

776777
# f.1) clearing only one subfigure
777-
subfigs[0].clear()
778+
getattr(subfigs[0], clear_meth)()
778779
assert subaxes[0] not in fig.axes
779780
assert subaxes[1] in fig.axes
780781
assert subfigs[1] in fig.subfigs
781782

782783
# f.2) clearing the whole thing
783-
subfigs[1].clear()
784+
getattr(subfigs[1], clear_meth)()
784785
subfigs = [fig.add_subfigure(gs[i]) for i in [0, 1]]
785786
subaxes = [sfig.add_subplot(111) for sfig in subfigs]
786787
assert all(ax in fig.axes for ax in subaxes)
787788
assert all(sfig in fig.subfigs for sfig in subfigs)
788-
fig.clear()
789+
getattr(fig, clear_meth)()
789790
assert fig.subfigs == []
790791
assert fig.axes == []
791792

792793

794+
def test_clf_not_refedined():
795+
for klass in FigureBase.__subclasses__():
796+
# check that subclasses do not get redefined in our Figure subclasses
797+
assert 'clf' not in klass.__dict__
798+
799+
793800
@mpl.style.context('mpl20')
794801
def test_picking_does_not_stale():
795802
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)