.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/animation/animate_decay.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_animation_animate_decay.py: ===== Decay ===== This example showcases: - using a generator to drive an animation, - changing axes limits during an animation. Output generated via `matplotlib.animation.Animation.to_jshtml`. .. GENERATED FROM PYTHON SOURCE LINES 13-60 .. container:: sphx-glr-animation .. raw:: html
.. code-block:: Python import itertools import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation def data_gen(): for cnt in itertools.count(): t = cnt / 10 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, 1) 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, # Only save last 100 frames, but run forever ani = animation.FuncAnimation(fig, run, data_gen, interval=100, init_func=init, save_count=100) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.351 seconds) .. _sphx_glr_download_gallery_animation_animate_decay.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: animate_decay.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: animate_decay.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_