10000 Merge pull request #2634 from tacaswell/animation_iter_length · matplotlib/matplotlib@397c27a · GitHub
[go: up one dir, main page]

Skip to content

Commit 397c27a

Browse files
committed
Merge pull request #2634 from tacaswell/animation_iter_length
address FuncAnimantion trying to take lengths of generators
2 parents a3abbb4 + 1b144e8 commit 397c27a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/matplotlib/animation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,8 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
10001000
self._iter_gen = frames
10011001
elif iterable(frames):
10021002
self._iter_gen = lambda: iter(frames)
1003-
self.save_count = len(frames)
1003+
if hasattr(frames, '__len__'):
1004+
self.save_count = len(frames)
10041005
else:
10051006
self._iter_gen = lambda: xrange(frames).__iter__()
10061007
self.save_count = frames

lib/matplotlib/tests/test_animation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ def animate(i):
5858
"see issues #1891 and #2679")
595 B63E 9

6060

61+
@cleanup
62+
def test_no_length_frames():
63+
fig, ax = plt.subplots()
64+
line, = ax.plot([], [])
65+
66+
def init():
67+
line.set_data([], [])
68+
return line,
69+
70+
def animate(i):
71+
x = np.linspace(0, 10, 100)
72+
y = np.sin(x + i)
73+
line.set_data(x, y)
74+
return line,
75+
76+
anim = animation.FuncAnimation(fig, animate, init_func=init,
77+
frames=iter(range(5)))
78+
79+
6180
if __name__ == "__main__":
6281
import nose
6382
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0