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

Skip to content

Commit 3cc3fca

Browse files
stanleyjstacaswell
authored andcommitted
FIX: clearing subfigures
1 parent 921e9ac commit 3cc3fca

File tree

3 files changed

+127
-21
lines changed

3 files changed

+127
-21
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Figure & subfigure clearing
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Fixed a bug in which ``Figure.clf()`` and ``Figure.clear()`` were broken
4+
when ``Figure`` contained axes not stored in ``Figure._localaxes``. Moved the
5+
``clear`` and ``clf`` methods to ``FigureBase``, so ``Subfigure``s can now be
6+
independently cleared. Added a routine to clear ``Figure.subfigs`` when
7+
``Figure.clear()`` is called. This effects the API as it removes active
8+
subfigure references when clearing a figure.

lib/matplotlib/figure.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,42 @@ 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):
917+
"""
918+
Clear the figure.
919+
Set *keep_observers* to True if, for example,
920+
a gui widget is tracking the Axes in the figure.
921+
"""
922+
self.suppressComposite = None
923+
self.callbacks = cbook.CallbackRegistry()
924+
925+
# first clear the axes in any subfigures
926+
for subfig in self.subfigs:
927+
subfig.clf(keep_observers=keep_observers)
928+
self.subfigs = []
929+
930+
for ax in tuple(self.axes): # Iterate over the copy.
931+
ax.cla()
932+
self.delaxes(ax) # Remove ax from self._axstack.
933+
self._axstack = _AxesStack()
934+
self.artists = []
935+
self.lines = []
936+
self.patches = []
937+
self.texts = []
938+
self.images = []
939+
self.legends = []
940+
if not keep_observers:
941+
self._axobservers = cbook.CallbackRegistry()
942+
self._suptitle = None
943+
self._supxlabel = None
944+
self._supylabel = None
945+
946+
self.stale = True
947+
948+
def clear(self, keep_observers=False):
949+
"""Clear the figure -- synonym for `clf`."""
950+
self.clf(keep_observers=keep_observers)
951+
916952
# Note: in the docstring below, the newlines in the examples after the
917953
# calls to legend() allow replacing it with figlegend() to generate the
918954
# docstring of pyplot.figlegend.
@@ -2807,30 +2843,12 @@ def clf(self, keep_observers=False):
28072843
Set *keep_observers* to True if, for example,
28082844
a gui widget is tracking the Axes in the figure.
28092845
"""
2810-
self.suppressComposite = None
2811-
self.callbacks = cbook.CallbackRegistry()
2812-
2813-
for ax in tuple(self.axes): # Iterate over the copy.
2814-
ax.cla()
2815-
self.delaxes(ax) # Remove ax from self._axstack.
2816-
2846+
super().clf(keep_observers=keep_observers)
2847+
# FigureBase.clf does not clear toolbars, as
2848+
# only Figure can have toolbars
28172849
toolbar = self.canvas.toolbar
28182850
if toolbar is not None:
28192851
toolbar.update()
2820-
self._axstack = _AxesStack()
2821-
self.artists = []
2822-
self.lines = []
2823-
self.patches = []
2824-
self.texts = []
2825-
self.images = []
2826-
self.legends = []
2827-
if not keep_observers:
2828-
self._axobservers = cbook.CallbackRegistry()
2829-
self._suptitle = None
2830-
self._supxlabel = None
2831-
self._supylabel = None
2832-
2833-
self.stale = True
28342852

28352853
def clear(self, keep_observers=False):
28362854
"""Clear the figure -- synonym for `clf`."""

lib/matplotlib/tests/test_figure.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,86 @@ def test_removed_axis():
709709
fig.canvas.draw()
710710

711711

712+
def test_figure_clear():
713+
# we test the following figure clearing scenarios:
714+
fig = plt.figure()
715+
716+
# a) an empty figure
717+
fig.clear()
718+
assert fig.axes == []
719+
720+
# b) a figure with a single unnested axes
721+
ax = fig.add_subplot(111)
722+
fig.clear()
723+
assert fig.axes == []
724+
725+
# c) a figure multiple unnested axes
726+
axes = [fig.add_subplot(2, 1, i+1) for i in range(2)]
727+
fig.clear()
728+
assert fig.axes == []
729+
730+
# d) a figure with a subfigure
731+
gs = fig.add_gridspec(ncols=2, nrows=1)
732+
subfig = fig.add_subfigure(gs[0])
733+
subaxes = subfig.add_subplot(111)
734+
fig.clear()
735+
assert subfig not in fig.subfigs
736+
assert fig.axes == []
737+
738+
# e) a figure with a subfigure and a subplot
739+
subfig = fig.add_subfigure(gs[0])
740+
subaxes = subfig.add_subplot(111)
741+
mainaxes = fig.add_subplot(gs[1])
742+
743+
# e.1) removing just the axis leaves the subplot
744+
mainaxes.remove()
745+
assert [subaxes] == fig.axes
746+
747+
# e.2) removing just the subaxis leaves the subplot
748+
# and subfigure
749+
mainaxes = fig.add_subplot(gs[1])
750+
subaxes.remove()
751+
assert [mainaxes] == fig.axes
752+
assert subfig in fig.subfigs
753+
754+
# e.3) clearing the subfigure leaves the subplot
755+
subaxes = subfig.add_subplot(111)
756+
assert all(ax in fig.axes for ax in [mainaxes, subaxes])
757+
subfig.clear()
758+
assert subfig in fig.subfigs
759+
assert subaxes not in subfig.axes
760+
assert subaxes not in fig.axes
761+
assert mainaxes in fig.axes
762+
763+
# e.4) clearing the whole thing
764+
subaxes = subfig.add_subplot(111)
765+
fig.clear()
766+
assert fig.axes == []
767+
assert fig.subfigs == []
768+
769+
# f) multiple subfigures
770+
subfigs = [fig.add_subfigure(gs[i]) for i in [0, 1]]
771+
subaxes = [sfig.add_subplot(111) for sfig in subfigs]
772+
assert all(ax in fig.axes for ax in subaxes)
773+
assert all(sfig in fig.subfigs for sfig in subfigs)
774+
775+
# f.1) clearing only one subfigure
776+
subfigs[0].clear()
777+
assert subaxes[0] not in fig.axes
778+
assert subaxes[1] in fig.axes
779+
assert subfigs[1] in fig.subfigs
780+
781+
# f.2) clearing the whole thing
782+
subfigs[1].clear()
783+
subfigs = [fig.add_subfigure(gs[i]) for i in [0, 1]]
784+
subaxes = [sfig.add_subplot(111) for sfig in subfigs]
785+
assert all(ax in fig.axes for ax in subaxes)
786+
assert all(sfig in fig.subfigs for sfig in subfigs)
787+
fig.clear()
788+
assert fig.subfigs == []
789+
assert fig.axes == []
790+
791+
712792
@mpl.style.context('mpl20')
713793
def test_picking_does_not_stale():
714794
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)