There was an error while loading. Please reload this page.
1 parent 49590bb commit 0bcfe16Copy full SHA for 0bcfe16
2 files changed
lib/matplotlib/axes/_base.py
@@ -1298,6 +1298,9 @@ def __clear(self):
1298
self.child_axes = []
1299
self._current_image = None # strictly for pyplot via _sci, _gci
1300
self._projection_init = None # strictly for pyplot.subplot
1301
+
1302
+ if hasattr(self, "legend_") and self.legend_ is not None:
1303
+ self.legend_.axes = self.legend_.figure = None
1304
self.legend_ = None
1305
self.containers = []
1306
lib/matplotlib/tests/test_axes.py
@@ -8644,13 +8644,19 @@ def test_extent_units():
8644
8645
def test_cla_clears_children_axes_and_fig():
8646
fig, ax = plt.subplots()
8647
- lines = ax.plot([], [], [], [])
8648
+ lines = ax.plot([], [], [], [], label="line")
8649
img = ax.imshow([[1]])
- for art in lines + [img]:
8650
+ legend = ax.legend()
8651
8652
+ children = [*lines, img, legend]
8653
8654
+ for art in children:
8655
assert art.axes is ax
8656
assert art.figure is fig
8657
8658
ax.clear()
8659
8660
assert art.axes is None
8661
assert art.figure is None
8662
0 commit comments