From d5e818231f8e05b194ffdc7e63c1834c39ff8edd Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 15 Jan 2020 00:08:04 +0100 Subject: [PATCH] Cleanup animation examples. - For many examples init_func doesn't do much, the default initial state is just fine. - animate_decay is fairly naturally an infinite animation, so don't limit it artificially to 1000 frames; also there is no need to disable blitting as that's already the default... and in fact blit=True would work just as well, but let's not bother. --- examples/animation/animate_decay.py | 7 ++++--- examples/animation/bayes_update.py | 10 +--------- examples/animation/double_pendulum_sgskip.py | 10 ++-------- examples/animation/simple_anim.py | 7 +------ 4 files changed, 8 insertions(+), 26 deletions(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index d292814a3c73..88f25e2d6aaa 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -8,13 +8,15 @@ - changing axes limits during an animation. """ +import itertools + import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def data_gen(): - for cnt in range(1000): + for cnt in itertools.count(): t = cnt / 10 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) @@ -47,6 +49,5 @@ def run(data): return line, -ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, - repeat=False, init_func=init) +ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init) plt.show() diff --git a/examples/animation/bayes_update.py b/examples/animation/bayes_update.py index 59462d3420f2..b8ce55946da7 100644 --- a/examples/animation/bayes_update.py +++ b/examples/animation/bayes_update.py @@ -38,16 +38,9 @@ def __init__(self, ax, prob=0.5): # which the plotted distribution should converge. self.ax.axvline(prob, linestyle='--', color='black') - def init(self): - self.success = 0 - self.line.set_data([], []) - return self.line, - def __call__(self, i): # This way the plot can continuously run and we just keep # watching new realizations of the process - if i == 0: - return self.init() # Choose success based on exceed a threshold with a uniform pick if np.random.rand(1,) < self.prob: @@ -62,6 +55,5 @@ def __call__(self, i): fig, ax = plt.subplots() ud = UpdateDist(ax, prob=0.7) -anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init, - interval=100, blit=True) +anim = FuncAnimation(fig, ud, frames=100, interval=100, blit=True) plt.show() diff --git a/examples/animation/double_pendulum_sgskip.py b/examples/animation/double_pendulum_sgskip.py index 55657f336e85..1dac5e171a0a 100644 --- a/examples/animation/double_pendulum_sgskip.py +++ b/examples/animation/double_pendulum_sgskip.py @@ -79,12 +79,6 @@ def derivs(state, t): time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) -def init(): - line.set_data([], []) - time_text.set_text('') - return line, time_text - - def animate(i): thisx = [0, x1[i], x2[i]] thisy = [0, y1[i], y2[i]] @@ -94,6 +88,6 @@ def animate(i): return line, time_text -ani = animation.FuncAnimation(fig, animate, range(1, len(y)), - interval=dt*1000, blit=True, init_func=init) +ani = animation.FuncAnimation( + fig, animate, len(y), interval=dt*1000, blit=True) plt.show() diff --git a/examples/animation/simple_anim.py b/examples/animation/simple_anim.py index 507d878bd183..173886e04c52 100644 --- a/examples/animation/simple_anim.py +++ b/examples/animation/simple_anim.py @@ -15,18 +15,13 @@ line, = ax.plot(x, np.sin(x)) -def init(): # only required for blitting to give a clean slate. - line.set_ydata([np.nan] * len(x)) - return line, - - def animate(i): line.set_ydata(np.sin(x + i / 100)) # update the data. return line, ani = animation.FuncAnimation( - fig, animate, init_func=init, interval=2, blit=True, save_count=50) + fig, animate, interval=2, blit=True, save_count=50) # To save the animation, use e.g. #