Skip to content

Commit a814673

Browse files
committed
FIX: make safe to add / remove artists during ArtistList iteration
If you remove artists during iteration some of the artists will be skipped due to the underlying list being mutated.
1 parent a76e037 commit a814673

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ def __len__(self):
13661366
for artist in self._axes._children)
13671367

13681368
def __iter__(self):
1369-
for artist in self._axes._children:
1369+
for artist in list(self._axes._children):
13701370
if self._type_check(artist):
13711371
yield artist
13721372

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7360,6 +7360,10 @@ def test_artist_sublists():
73607360
assert ax.lines + [1, 2, 3] == [*lines, 1, 2, 3]
73617361
assert [1, 2, 3] + ax.lines == [1, 2, 3, *lines]
73627362

7363+
for ln in ax.lines:
7364+
ln.remove()
7365+
assert len(ax.lines) == 0
7366+
73637367

73647368
def test_empty_line_plots():
73657369
# Incompatible nr columns, plot "nothing"

0 commit comments

Comments
 (0)