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

Skip to content

Commit a264c0f

Browse files
committed
Simplify repeat_delay and fix support for it when using iterable frames.
- Simplify the implementation of `repeat_delay`: instead of switching callbacks back and forth, just set the timer interval at each iteration to either the normal delay or the end-of-loop delay. - Fix support for `repeat_delay` when `frames` is an iterable: the previous implementation handled looping in `iter_frames` and therefore `_step` never saw the end of the iterable. Instead, just iterate through `frames` once in `iter_frames`, but still `tee` the iterable so that `_step` can take care of repeating the animation. Can be checked with ```python import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() line, = ax.plot(range(10)) def animate(i): line.set_ydata(range(i, i + 10)) # update the data. return line, ani = animation.FuncAnimation( fig, animate, frames=range(5), interval=1000, repeat_delay=2000, blit=True, save_count=50) plt.show() ``` Previously repeat_delay would not be applied.
1 parent 07145c2 commit a264c0f

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

lib/matplotlib/animation.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,27 +1433,12 @@ def _step(self, *args):
14331433
if not still_going and self.repeat:
14341434
self._init_draw()
14351435
self.frame_seq = self.new_frame_seq()
1436-
self.event_source.remove_callback(self._step)
1437-
self.event_source.add_callback(self._loop_delay)
14381436
self.event_source.interval = self._repeat_delay
14391437
return True
14401438
else:
1439+
self.event_source.interval = self._interval
14411440
return still_going
14421441

1443-
def _stop(self, *args):
1444-
# If we stop in the middle of a loop delay (which is relatively likely
1445-
# given the potential pause here), remove the loop_delay callback as
1446-
# well.
1447-
self.event_source.remove_callback(self._loop_delay)
1448-
super()._stop()
1449-
1450-
def _loop_delay(self, *args):
1451-
# Reset the interval and change callbacks after the delay.
1452-
self.event_source.remove_callback(self._loop_delay)
1453-
self.event_source.interval = self._interval
1454-
self.event_source.add_callback(self._step)
1455-
Animation._step(self)
1456-
14571442

14581443
class ArtistAnimation(TimedAnimation):
14591444
"""
@@ -1633,10 +1618,10 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
16331618
self._iter_gen = frames
16341619
elif np.iterable(frames):
16351620
if kwargs.get('repeat', True):
1621+
self._tee_from = frames
16361622
def iter_frames(frames=frames):
1637-
while True:
1638-
this, frames = itertools.tee(frames, 2)
1639-
yield from this
1623+
this, self._tee_from = itertools.tee(self._tee_from, 2)
1624+
yield from this
16401625
self._iter_gen = iter_frames
16411626
else:
16421627
self._iter_gen = lambda: iter(frames)

0 commit comments

Comments
 (0)