From ffcc8d314c8a47772ba541027f138ee18155d7e6 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 4 Dec 2022 18:53:58 -0500 Subject: [PATCH 1/3] MNT: when clearing an Axes via clear/cla fully detach children Reset the Axes and Figure of the children to None to help break cycles. Closes #6982 --- lib/matplotlib/axes/_base.py | 3 +++ lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 574310ea763a..1f46c6f7444c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1315,7 +1315,10 @@ def __clear(self): self._get_patches_for_fill = _process_plot_var_args(self, 'fill') self._gridOn = mpl.rcParams['axes.grid'] + old_children = getattr(self, '_children', []) self._children = [] + for chld in old_children: + chld.axes = chld.figure = None self._mouseover_set = _OrderedSet() self.child_axes = [] self._current_image = None # strictly for pyplot via _sci, _gci diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index deb6c3fb2ba1..a5f1be66ce1c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8359,6 +8359,19 @@ def test_extent_units(): im.set_extent([2, 12, date_first, date_last], clip=False) +def test_cla_clears_chlidren_axes_and_fig(): + fig, ax = plt.subplots() + lines = ax.plot([], [], [], []) + img = ax.imshow([[1]]) + for art in lines + [img]: + assert art.axes is ax + assert art.figure is fig + ax.clear() + for art in lines + [img]: + assert art.axes is None + assert art.figure is None + + def test_scatter_color_repr_error(): def get_next_color(): From 146b478cc16e648fbd3e3f5629a4b9fed0e53b0d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 5 Dec 2022 10:01:51 -0500 Subject: [PATCH 2/3] MNT: compress swap to one line --- lib/matplotlib/axes/_base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 1f46c6f7444c..4ea2b2433706 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1315,8 +1315,7 @@ def __clear(self): self._get_patches_for_fill = _process_plot_var_args(self, 'fill') self._gridOn = mpl.rcParams['axes.grid'] - old_children = getattr(self, '_children', []) - self._children = [] + old_children, self._children = self._children, [] for chld in old_children: chld.axes = chld.figure = None self._mouseover_set = _OrderedSet() From c77fa4c05b52481062c60d5b26a88ceadd753674 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 7 Dec 2022 16:12:50 -0500 Subject: [PATCH 3/3] DOC: fix spelling in test Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a5f1be66ce1c..680602f706e7 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8359,7 +8359,7 @@ def test_extent_units(): im.set_extent([2, 12, date_first, date_last], clip=False) -def test_cla_clears_chlidren_axes_and_fig(): +def test_cla_clears_children_axes_and_fig(): fig, ax = plt.subplots() lines = ax.plot([], [], [], []) img = ax.imshow([[1]])