From 15c6d59e9896a379ddeaa57fae25ae41fe10a4d8 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 22 Jan 2018 22:28:19 -0800 Subject: [PATCH] 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). --- examples/animation/simple_anim.py | 2 +- lib/matplotlib/animation.py | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/examples/animation/simple_anim.py b/examples/animation/simple_anim.py index 2b0f02849f0f..59552cd1cfa7 100644 --- a/examples/animation/simple_anim.py +++ b/examples/animation/simple_anim.py @@ -26,7 +26,7 @@ def animate(i): ani = animation.FuncAnimation( - fig, animate, init_func=init, interval=2, blit=True) + fig, animate, init_func=init, interval=2, blit=True, save_count=50) # To save the animation, use e.g. # diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index fc291e5152e7..911ee5445947 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -38,9 +38,8 @@ from matplotlib._animation_data import (DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE) -from matplotlib.cbook import iterable, deprecated from matplotlib.compat import subprocess -from matplotlib import rcParams, rcParamsDefault, rc_context +from matplotlib import cbook, rcParams, rcParamsDefault, rc_context if six.PY2: from base64 import encodestring as encodebytes @@ -1681,7 +1680,7 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None, self._iter_gen = itertools.count elif callable(frames): self._iter_gen = frames - elif iterable(frames): + elif cbook.iterable(frames): self._iter_gen = lambda: iter(frames) if hasattr(frames, '__len__'): self.save_count = len(frames) @@ -1723,7 +1722,26 @@ def new_saved_frame_seq(self): self._old_saved_seq = list(self._save_seq) return iter(self._old_saved_seq) else: - return itertools.islice(self.new_frame_seq(), self.save_count) + if self.save_count is not None: + return itertools.islice(self.new_frame_seq(), self.save_count) + + else: + frame_seq = self.new_frame_seq() + + def gen(): + try: + for _ in range(100): + yield next(frame_seq) + except StopIteration: + pass + else: + cbook.warn_deprecated( + "2.2", "FuncAnimation.save has truncated your " + "animation to 100 frames. In the future, no such " + "truncation will occur; please pass 'save_count' " + "accordingly.") + + return gen() def _init_draw(self): # Initialize the drawing either using the given init_func or by