diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index 77c78c2dae71..85e289b0096f 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -129,9 +129,9 @@ Examples .. toctree:: :maxdepth: 1 + ../gallery/animation/animate_decay ../gallery/animation/bayes_update ../gallery/animation/double_pendulum_sgskip - ../gallery/animation/dynamic_image ../gallery/animation/histogram ../gallery/animation/rain ../gallery/animation/random_walk @@ -148,7 +148,7 @@ Examples .. toctree:: :maxdepth: 1 - ../gallery/animation/dynamic_image2 + ../gallery/animation/dynamic_image Writer Classes ============== diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py new file mode 100644 index 000000000000..b4b2aa091a15 --- /dev/null +++ b/examples/animation/animate_decay.py @@ -0,0 +1,54 @@ +""" +===== +Decay +===== + +This example showcases: +- using a generator to drive an animation, +- changing axes limits during an animation. +""" + +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation + + +def data_gen(t=0): + cnt = 0 + while cnt < 1000: + cnt += 1 + 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.grid() +xdata, ydata = [], [] + + +def run(data): + # update the data + t, y = data + xdata.append(t) + ydata.append(y) + xmin, xmax = ax.get_xlim() + + if t >= xmax: + ax.set_xlim(xmin, 2*xmax) + ax.figure.canvas.draw() + line.set_data(xdata, ydata) + + return line, + +ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, + repeat=False, init_func=init) +plt.show() diff --git a/examples/animation/dynamic_image.py b/examples/animation/dynamic_image.py index f6083b0957ed..d82e62f62af6 100644 --- a/examples/animation/dynamic_image.py +++ b/examples/animation/dynamic_image.py @@ -1,7 +1,7 @@ """ -============== -Animated image -============== +================================================= +Animated image using a precomputed list of images +================================================= """ @@ -17,18 +17,18 @@ def f(x, y): x = np.linspace(0, 2 * np.pi, 120) y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) - -im = plt.imshow(f(x, y), animated=True) - - -def updatefig(*args): - global x, y +# ims is a list of lists, each row is a list of artists to draw in the +# current frame; here we are just animating one artist, the image, in +# each frame +ims = [] +for i in range(60): x += np.pi / 15. y += np.pi / 20. - im.set_array(f(x, y)) - return im, + im = plt.imshow(f(x, y), animated=True) + ims.append([im]) -ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) +ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, + repeat_delay=1000) # To save the animation, use e.g. # diff --git a/examples/animation/dynamic_image2.py b/examples/animation/dynamic_image2.py deleted file mode 100644 index d82e62f62af6..000000000000 --- a/examples/animation/dynamic_image2.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -================================================= -Animated image using a precomputed list of images -================================================= - -""" - -import numpy as np -import matplotlib.pyplot as plt -import matplotlib.animation as animation - -fig = plt.figure() - - -def f(x, y): - return np.sin(x) + np.cos(y) - -x = np.linspace(0, 2 * np.pi, 120) -y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) -# ims is a list of lists, each row is a list of artists to draw in the -# current frame; here we are just animating one artist, the image, in -# each frame -ims = [] -for i in range(60): - x += np.pi / 15. - y += np.pi / 20. - im = plt.imshow(f(x, y), animated=True) - ims.append([im]) - -ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, - repeat_delay=1000) - -# To save the animation, use e.g. -# -# ani.save("movie.mp4") -# -# or -# -# from matplotlib.animation import FFMpegWriter -# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800) -# ani.save("movie.mp4", writer=writer) - -plt.show()