diff --git a/doc/api/animation_api.rst b/doc/api/animation_api.rst index 2e052b53a5ce..1d4bb34bb5f7 100644 --- a/doc/api/animation_api.rst +++ b/doc/api/animation_api.rst @@ -103,6 +103,7 @@ artist at a global scope and let Python sort things out. For example :: import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation + from functools import partial fig, ax = plt.subplots() xdata, ydata = [], [] @@ -123,8 +124,37 @@ artist at a global scope and let Python sort things out. For example :: init_func=init, blit=True) plt.show() -The second method is to use `functools.partial` to 'bind' artists to -function. A third method is to use closures to build up the required +The second method is to use `functools.partial` to pass arguments to the +function. :: + + import numpy as np + import matplotlib.pyplot as plt + from matplotlib.animation import FuncAnimation + from functools import partial + + fig, ax = plt.subplots() + ln, = plt.plot([], [], 'ro') + + def init(): + ax.set_xlim(0, 2*np.pi) + ax.set_ylim(-1, 1) + return ln, + + def update(frame, x, y): + x.append(frame) + y.append(np.sin(frame)) + ln.set_data(xdata, ydata) + return ln, + + xdata, ydata = [], [] + ani = FuncAnimation( + fig, partial(update, x=xdata, y=ydata), + frames=np.linspace(0, 2 * np.pi, 128), + init_func=init, blit=True) + + plt.show() + +A third method is to use closures to build up the required artists and functions. A fourth method is to create a class. Examples diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index c15fa086eb15..c96923bb86b3 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1622,7 +1622,8 @@ def init_func() -> iterable_of_artists value is unused if ``blit == False`` and may be omitted in that case. fargs : tuple or None, optional - Additional arguments to pass to each call to *func*. + Additional arguments to pass to each call to *func*. Note: the use of + `functools.partial` is preferred over *fargs*. save_count : int, default: 100 Fallback for the number of values from *frames* to cache. This is