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

Skip to content

Commit 5c58ea9

Browse files
committed
Merge pull request #4800 from jenshnielsen/funcanimfixes
FIX: Fixes to funcanimation
2 parents 491137e + b4b0c91 commit 5c58ea9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

examples/animation/animate_decay.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ def data_gen(t=0):
1010
t += 0.1
1111
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
1212

13+
14+
def init():
15+
ax.set_ylim(-1.1, 1.1)
16+
ax.set_xlim(0, 10)
17+
del xdata[:]
18+
del ydata[:]
19+
line.set_data(xdata, ydata)
20+
return line,
21+
1322
fig, ax = plt.subplots()
1423
line, = ax.plot([], [], lw=2)
15-
ax.set_ylim(-1.1, 1.1)
16-
ax.set_xlim(0, 5)
1724
ax.grid()
1825
xdata, ydata = [], []
1926

@@ -33,5 +40,5 @@ def run(data):
3340
return line,
3441

3542
ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
36-
repeat=False)
43+
repeat=False, init_func=init)
3744
plt.show()

lib/matplotlib/animation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ def _start(self, *args):
613613
# actually start the event_source. We also disconnect _start
614614
# from the draw_events
615615
self.event_source.add_callback(self._step)
616+
self._init_draw()
616617
self.event_source.start()
617618
self._fig.canvas.mpl_disconnect(self._first_draw_id)
618619
self._first_draw_id = None # So we can check on save
@@ -762,6 +763,9 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
762763
# since GUI widgets are gone. Either need to remove extra code to
763764
# allow for this non-existant use case or find a way to make it work.
764765
with writer.saving(self._fig, filename, dpi):
766+
for anim in all_anim:
767+
# Clear the initial frame
768+
anim._init_draw()
765769
for data in zip(*[a.new_saved_frame_seq()
766770
for a in all_anim]):
767771
for anim, d in zip(all_anim, data):
@@ -1135,7 +1139,10 @@ def new_saved_frame_seq(self):
11351139
# no saved frames, generate a new frame sequence and take the first
11361140
# save_count entries in it.
11371141
if self._save_seq:
1138-
return iter(self._save_seq)
1142+
# While iterating we are going to update _save_seq
1143+
# so make a copy to safely iterate over
1144+
self._old_saved_seq = self._save_seq.copy()
1145+
return iter(self._old_saved_seq)
11391146
else:
11401147
return itertools.islice(self.new_frame_seq(), self.save_count)
11411148

0 commit comments

Comments
 (0)