Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b2777b

Browse filesBrowse files
committed
MNT: prefer "clear" as cannonical over clf
1 parent a395083 commit 6b2777b
Copy full SHA for 6b2777b

File tree

Expand file treeCollapse file tree

2 files changed

+22
-18
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+22
-18
lines changed

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+11-8Lines changed: 11 additions & 8 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,8 +949,8 @@ def clf(self, keep_observers=False):
949949

950950
self.stale = True
951951

952-
# synonym for `clf`."""
953-
clear = clf
952+
# synonym for `clear`.
953+
clf = clear
954954

955955
# Note: in the docstring below, the newlines in the examples after the
956956
# calls to legend() allow replacing it with figlegend() to generate the
@@ -2340,7 +2340,7 @@ def __init__(self,
23402340
self.subplotpars = subplotpars
23412341

23422342
self._axstack = _AxesStack() # track all figure axes and current axes
2343-
self.clf()
2343+
self.clear()
23442344
self._cachedRenderer = None
23452345

23462346
# list of child gridspecs for this figure
@@ -2839,15 +2839,18 @@ def set_figheight(self, val, forward=True):
28392839
"""
28402840
self.set_size_inches(self.get_figwidth(), val, forward=forward)
28412841

2842-
def clf(self, keep_observers=False):
2842+
def clear(self, keep_observers=False):
28432843
# docstring inherited
2844-
super().clf(keep_observers=keep_observers)
2845-
# FigureBase.clf does not clear toolbars, as
2844+
super().clear(keep_observers=keep_observers)
2845+
# FigureBase.clear does not clear toolbars, as
28462846
# only Figure can have toolbars
28472847
toolbar = self.canvas.toolbar
28482848
if toolbar is not None:
28492849
toolbar.update()
28502850

2851+
# synonym for `clear`."""
2852+
clf = clear
2853+
28512854
@_finalize_rasterization
28522855
@allow_rasterization
28532856
def draw(self, renderer):

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+11-10Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -709,29 +709,30 @@ def test_removed_axis():
709709
fig.canvas.draw()
710710

711711

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

716717
# a) an empty figure
717-
fig.clear()
718+
getattr(fig, clear_method)()
718719
assert fig.axes == []
719720

720721
# b) a figure with a single unnested axes
721722
ax = fig.add_subplot(111)
722-
fig.clear()
723+
getattr(fig, clear_method)()
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_method)()
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_method)()
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_method)()
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_method)()
767768
assert fig.axes == []
768769
assert fig.subfigs == []
769770

@@ -774,18 +775,18 @@ 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_method)()
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_method)()
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_method)()
789790
assert fig.subfigs == []
790791
assert fig.axes == []
791792

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.