Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5471db1

Browse files
committed
FIX: don't fail on first show if animation already exhausted
If we need to get the first frame in `_init_draw()` (because the user did not pass an initialization function) fails, warn instead of raising. closes #17770
1 parent ec126b0 commit 5471db1

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/matplotlib/animation.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,8 +1666,21 @@ def _init_draw(self):
16661666
# For blitting, the init_func should return a sequence of modified
16671667
# artists.
16681668
if self._init_func is None:
1669-
self._draw_frame(next(self.new_frame_seq()))
1670-
1669+
try:
1670+
frame_data = next(self.new_frame_seq())
1671+
except StopIteration:
1672+
# we can't start the iteration, it may have already been
1673+
# exhausted by a previous save or just be 0 length.
1674+
# warn and bail.
1675+
warnings.warn(
1676+
"Can not start iterating the frames for the initial draw. "
1677+
"This can be caused by passing in a 0 length sequence "
1678+
"for *frames*.\n\n"
1679+
"If you passed *frames* as a generator "
1680+
"it may be exhausted due to a previous display or save."
1681+
)
1682+
return
1683+
self._draw_frame(frame_data)
16711684
else:
16721685
self._drawn_artists = self._init_func()
16731686
if self._blit:

lib/matplotlib/tests/test_animation.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,36 @@ def animate(i):
359359

360360
with pytest.raises(RuntimeError):
361361
animation.FuncAnimation(fig, animate, blit=True)
362+
363+
364+
def test_exhausted_animation(tmpdir):
365+
fig, ax = plt.subplots()
366+
367+
def update(frame):
368+
return []
369+
370+
anim = animation.FuncAnimation(
371+
fig, update, frames=iter(range(10)), repeat=False,
372+
cache_frame_data=False
373+
)
374+
375+
with tmpdir.as_cwd():
376+
anim.save("test.gif", writer='pillow')
377+
378+
with pytest.warns(UserWarning, match="exhausted"):
379+
anim._start()
380+
381+
382+
def test_no_frame_warning(tmpdir):
383+
fig, ax = plt.subplots()
384+
385+
def update(frame):
386+
return []
387+
388+
anim = animation.FuncAnimation(
389+
fig, update, frames=[], repeat=False,
390+
cache_frame_data=False
391+
)
392+
393+
with pytest.warns(UserWarning, match="exhausted"):
394+
anim._start()

0 commit comments

Comments
 (0)