Skip to content

Commit ffcc8d3

Browse files
committed
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
1 parent 0aac9f1 commit ffcc8d3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/matplotlib/axes/_base.py

+3
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,10 @@ def __clear(self):
13151315
self._get_patches_for_fill = _process_plot_var_args(self, 'fill')
13161316

13171317
self._gridOn = mpl.rcParams['axes.grid']
1318+
old_children = getattr(self, '_children', [])
13181319
self._children = []
1320+
for chld in old_children:
1321+
chld.axes = chld.figure = None
13191322
self._mouseover_set = _OrderedSet()
13201323
self.child_axes = []
13211324
self._current_image = None # strictly for pyplot via _sci, _gci

lib/matplotlib/tests/test_axes.py

+13
Original file line numberDiff line numberDiff line change
@@ -8359,6 +8359,19 @@ def test_extent_units():
83598359
im.set_extent([2, 12, date_first, date_last], clip=False)
83608360

83618361

8362+
def test_cla_clears_chlidren_axes_and_fig():
8363+
fig, ax = plt.subplots()
8364+
lines = ax.plot([], [], [], [])
8365+
img = ax.imshow([[1]])
8366+
for art in lines + [img]:
8367+
assert art.axes is ax
8368+
assert art.figure is fig
8369+
ax.clear()
8370+
for art in lines + [img]:
8371+
assert art.axes is None
8372+
assert art.figure is None
8373+
8374+
83628375
def test_scatter_color_repr_error():
83638376

83648377
def get_next_color():

0 commit comments

Comments
 (0)