12.3.10.1.7. Function animation#

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
(ln,) = plt.plot([], [], "ro", animated=True)


def init():
    ax.set_xlim(0, 2 * np.pi)
    ax.set_ylim(-1, 1)
    return (ln,)


def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return (ln,)


ani = FuncAnimation(
    fig,
    update,
    frames=np.linspace(0, 2 * np.pi, 128),
    init_func=init,
    blit=True,
)
plt.show()

Total running time of the script: (0 minutes 7.716 seconds)