From 6f674d5cdb9e005861da0690966d17f37ec6ce07 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Sun, 26 Jul 2015 15:31:48 +0100 Subject: [PATCH 1/4] Make sure that animations are probably cleared before rerunning by calling init both when showing and saving --- lib/matplotlib/animation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 99b6c4d8d23d..93fa19927e76 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -611,6 +611,7 @@ def _start(self, *args): # actually start the event_source. We also disconnect _start # from the draw_events self.event_source.add_callback(self._step) + self._init_draw() self.event_source.start() self._fig.canvas.mpl_disconnect(self._first_draw_id) self._first_draw_id = None # So we can check on save @@ -760,6 +761,9 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None, # since GUI widgets are gone. Either need to remove extra code to # allow for this non-existant use case or find a way to make it work. with writer.saving(self._fig, filename, dpi): + for anim in all_anim: + # Clear the initial frame + anim._init_draw() for data in zip(*[a.new_saved_frame_seq() for a in all_anim]): for anim, d in zip(all_anim, data): From 2b77d8e32898c5e5ef43c1dd5361bb38ceae073a Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Mon, 27 Jul 2015 21:41:07 +0100 Subject: [PATCH 2/4] Make a copy of _save_seq to iterate over --- lib/matplotlib/animation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 93fa19927e76..4115a3e3ce65 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1084,7 +1084,10 @@ def new_saved_frame_seq(self): # no saved frames, generate a new frame sequence and take the first # save_count entries in it. if self._save_seq: - return iter(self._save_seq) + # While iterating we are going to update _save_seq + # so make a copy to safely iterate over + self._old_saved_seq = self._save_seq.copy() + return iter(self._old_saved_seq) else: return itertools.islice(self.new_frame_seq(), self.save_count) From aa04769b771b692e7dab05e810e5e051a1404aa8 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Mon, 27 Jul 2015 21:54:57 +0100 Subject: [PATCH 3/4] Add init function to anim decay example Ensures that the plot is cleared before restarting the animation --- examples/animation/animate_decay.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index 71f1eb5d341a..33b558cc2520 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -10,10 +10,16 @@ def data_gen(t=0): t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) +def init(): + ax.set_ylim(-1.1, 1.1) + ax.set_xlim(0, 10) + del xdata[:] + del ydata[:] + line.set_data(xdata,ydata) + return line, + fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) -ax.set_ylim(-1.1, 1.1) -ax.set_xlim(0, 5) ax.grid() xdata, ydata = [], [] @@ -33,5 +39,5 @@ def run(data): return line, ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, - repeat=False) + repeat=False, init_func=init) plt.show() From b4b0c91e2f15f37f7a06364a629652467cce8cc4 Mon Sep 17 00:00:00 2001 From: Jens Hedegaard Nielsen Date: Mon, 27 Jul 2015 22:34:06 +0100 Subject: [PATCH 4/4] PEP8 --- examples/animation/animate_decay.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index 33b558cc2520..3ddcbdf2408e 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -10,12 +10,13 @@ def data_gen(t=0): t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) + def init(): ax.set_ylim(-1.1, 1.1) ax.set_xlim(0, 10) del xdata[:] del ydata[:] - line.set_data(xdata,ydata) + line.set_data(xdata, ydata) return line, fig, ax = plt.subplots()