Skip to content

Commit acb6d5c

Browse files
committed
Deprecate truncating saved unsized anims to 100 frames.
To test that the deprecation is working, uncomment the `ani.save(...)` line in the simple_anim.py example and modify the anim's save_count. Animations that happened to generate exactly 100 frames without truncation will incorrectly trigger a warning; not sure we can do much about it (because after the 100th frame is generated there is no way to know whether another frame is coming except by actually calling next() on the generator, which we do not want to do).
1 parent 4753f6c commit acb6d5c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

examples/animation/simple_anim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def animate(i):
2626

2727

2828
ani = animation.FuncAnimation(
29-
fig, animate, init_func=init, interval=2, blit=True)
29+
fig, animate, init_func=init, interval=2, blit=True, save_count=50)
3030

3131
# To save the animation, use e.g.
3232
#

lib/matplotlib/animation.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838

3939
from matplotlib._animation_data import (DISPLAY_TEMPLATE, INCLUDED_FRAMES,
4040
JS_INCLUDE)
41-
from matplotlib.cbook import iterable, deprecated
4241
from matplotlib.compat import subprocess
43-
from matplotlib import rcParams, rcParamsDefault, rc_context
42+
from matplotlib import cbook, rcParams, rcParamsDefault, rc_context
4443

4544
if six.PY2:
4645
from base64 import encodestring as encodebytes
@@ -1681,7 +1680,7 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
16811680
self._iter_gen = itertools.count
16821681
elif callable(frames):
16831682
self._iter_gen = frames
1684-
elif iterable(frames):
1683+
elif cbook.iterable(frames):
16851684
self._iter_gen = lambda: iter(frames)
16861685
if hasattr(frames, '__len__'):
16871686
self.save_count = len(frames)
@@ -1723,7 +1722,25 @@ def new_saved_frame_seq(self):
17231722
self._old_saved_seq = list(self._save_seq)
17241723
return iter(self._old_saved_seq)
17251724
else:
1726-
return itertools.islice(self.new_frame_seq(), self.save_count)
1725+
it = itertools.islice(self.new_frame_seq(), self.save_count)
1726+
1727+
def gen():
1728+
try:
1729+
for _ in range(100):
1730+
yield next(it)
1731+
except StopIteration:
1732+
return
1733+
cbook.warn_deprecated(
1734+
"2.2", "FuncAnimation.save has truncated your animation "
1735+
"to 100 frames. In the future, no such truncation will "
1736+
"occur; please pass 'save_count' accordingly.")
1737+
try:
1738+
while True:
1739+
yield next(it)
1740+
except StopIteration:
1741+
return
1742+
1743+
return gen()
17271744

17281745
def _init_draw(self):
17291746
# Initialize the drawing either using the given init_func or by

0 commit comments

Comments
 (0)