Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example and docstring to encourage the use of functools.partial in FuncAnimation #24238

Merged
merged 4 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions doc/api/animation_api.rst
Expand Up @@ -133,8 +133,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')
oscargus marked this conversation as resolved.
Show resolved Hide resolved

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

def update(frame, x, y):
oscargus marked this conversation as resolved.
Show resolved Hide resolved
x.append(frame)
y.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,

xdata, ydata = [], []
oscargus marked this conversation as resolved.
Show resolved Hide resolved
ani = FuncAnimation(
fig, partial(update, x=xdata, y=ydata),
oscargus marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
17 changes: 15 additions & 2 deletions lib/matplotlib/animation.py
Expand Up @@ -1520,12 +1520,24 @@ class FuncAnimation(TimedAnimation):
func : callable
The function to call at each frame. The first argument will
be the next value in *frames*. Any additional positional
arguments can be supplied via the *fargs* parameter.
arguments can be supplied using `functools.partial` or via the *fargs*
parameter.

The required signature is::

def func(frame, *fargs) -> iterable_of_artists

It is often more convenient to provide the arguments using
`functools.partial`. In this way it is also possible to pass keyword
arguments. To pass a function with both positional and keyword
arguments, set all arguments as keyword arguments, just leaving the
*frame* argument unset::

def func(frame, x, *, y=None):
oscargus marked this conversation as resolved.
Show resolved Hide resolved
...

ani = FuncAnimation(fig, partial(func, x=1, y='foo'))
oscargus marked this conversation as resolved.
Show resolved Hide resolved

If ``blit == True``, *func* must return an iterable of all artists
that were modified or created. This information is used by the blitting
algorithm to determine which parts of the figure have to be updated.
Expand Down Expand Up @@ -1564,7 +1576,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*. See *func* for details.

save_count : int, default: 100
Fallback for the number of values from *frames* to cache. This is
Expand Down