Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 185a421

Browse files
authored
Merge pull request #24238 from oscargus/mpl-20326
Update example and docstring to encourage the use of functools.partial in FuncAnimation
2 parents 8937d7b + d97cc32 commit 185a421

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

doc/api/animation_api.rst

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ this means that the callable objects you pass in must know what
108108
artists they should be working on. There are several approaches to
109109
handling this, of varying complexity and encapsulation. The simplest
110110
approach, which works quite well in the case of a script, is to define the
111-
artist at a global scope and let Python sort things out. For example ::
111+
artist at a global scope and let Python sort things out. For example::
112112

113113
import numpy as np
114114
import matplotlib.pyplot as plt
@@ -133,8 +133,36 @@ artist at a global scope and let Python sort things out. For example ::
133133
init_func=init, blit=True)
134134
plt.show()
135135

136-
The second method is to use `functools.partial` to 'bind' artists to
137-
function. A third method is to use closures to build up the required
136+
The second method is to use `functools.partial` to pass arguments to the
137+
function::
138+
139+
import numpy as np
140+
import matplotlib.pyplot as plt
141+
from matplotlib.animation import FuncAnimation
142+
from functools import partial
143+
144+
fig, ax = plt.subplots()
145+
line1, = ax.plot([], [], 'ro')
146+
147+
def init():
148+
ax.set_xlim(0, 2*np.pi)
149+
ax.set_ylim(-1, 1)
150+
return line1,
151+
152+
def update(frame, ln, x, y):
153+
x.append(frame)
154+
y.append(np.sin(frame))
155+
ln.set_data(x, y)
156+
return ln,
157+
158+
ani = FuncAnimation(
159+
fig, partial(update, ln=line1, x=[], y=[]),
160+
frames=np.linspace(0, 2*np.pi, 128),
161+
init_func=init, blit=True)
162+
163+
plt.show()
164+
165+
A third method is to use closures to build up the required
138166
artists and functions. A fourth method is to create a class.
139167

140168
Examples

lib/matplotlib/animation.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,12 +1519,24 @@ class FuncAnimation(TimedAnimation):
15191519
func : callable
15201520
The function to call at each frame. The first argument will
15211521
be the next value in *frames*. Any additional positional
1522-
arguments can be supplied via the *fargs* parameter.
1522+
arguments can be supplied using `functools.partial` or via the *fargs*
1523+
parameter.
15231524
15241525
The required signature is::
15251526
15261527
def func(frame, *fargs) -> iterable_of_artists
15271528
1529+
It is often more convenient to provide the arguments using
1530+
`functools.partial`. In this way it is also possible to pass keyword
1531+
arguments. To pass a function with both positional and keyword
1532+
arguments, set all arguments as keyword arguments, just leaving the
1533+
*frame* argument unset::
1534+
1535+
def func(frame, art, *, y=None):
1536+
...
1537+
1538+
ani = FuncAnimation(fig, partial(func, art=ln, y='foo'))
1539+
15281540
If ``blit == True``, *func* must return an iterable of all artists
15291541
that were modified or created. This information is used by the blitting
15301542
algorithm to determine which parts of the figure have to be updated.
@@ -1563,7 +1575,8 @@ def init_func() -> iterable_of_artists
15631575
value is unused if ``blit == False`` and may be omitted in that case.
15641576
15651577
fargs : tuple or None, optional
1566-
Additional arguments to pass to each call to *func*.
1578+
Additional arguments to pass to each call to *func*. Note: the use of
1579+
`functools.partial` is preferred over *fargs*. See *func* for details.
15671580
15681581
save_count : int, default: 100
15691582
Fallback for the number of values from *frames* to cache. This is

0 commit comments

Comments
 (0)