Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,15 @@ def __clear(self):
self._get_lines = _process_plot_var_args(self)
self._get_patches_for_fill = _process_plot_var_args(self, 'fill')

legend = getattr(self, "legend_", None)
if legend is not None:
legend.remove()

for child in list(getattr(self, "_children", ())):
if child is legend:
continue
child.remove()

self._gridOn = mpl.rcParams['axes.grid']
self._children = []
self._mouseover_set = _OrderedSet()
Expand Down
14 changes: 13 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,19 @@ def clear(self, keep_observers=False):

for ax in tuple(self.axes): # Iterate over the copy.
ax.clear()
self.delaxes(ax) # Remove ax from self._axstack.
ax.remove()

for artist_list in (
self.artists,
self.lines,
self.patches,
self.texts,
self.images,
self.legends):
for artist in list(artist_list):
artist.remove()
if getattr(artist, "figure", None) is not None:
artist.figure = None

self.artists = []
self.lines = []
Expand Down
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,41 @@ def test_remove():
assert ax.stale


@pytest.mark.backend("Agg")
def test_cla_unsets_artist_axes():
fig, ax = plt.subplots()
line, = ax.plot([0, 1], [0, 1])
patch = ax.add_patch(mpatches.Rectangle((0, 0), 1, 1))
image = ax.imshow(np.arange(4).reshape(2, 2))
text = ax.text(0.5, 0.5, "hi")
collection = ax.scatter([0], [0])
legend = ax.legend([line], ["line"])

ax.cla()

for artist in (line, patch, image, text, collection, legend):
assert artist.axes is None


@pytest.mark.backend("Agg")
def test_clf_unsets_figure_and_axes_parents():
fig, ax = plt.subplots()
line, = ax.plot([0, 1], [0, 1])
image = ax.imshow(np.arange(4).reshape(2, 2))
fig_text = fig.text(0.5, 0.5, "hi")
fig_legend = fig.legend([line], ["line"])
colorbar = fig.colorbar(image, ax=ax)

fig.clf()

assert fig.axes == []
assert line.axes is None
assert image.axes is None
assert fig_text.figure is None
assert fig_legend.figure is None
assert colorbar.ax.figure is None


@image_comparison(["default_edges.png"], remove_text=True, style='default')
def test_default_edges():
# Remove this line when this test image is regenerated.
Expand Down