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

Skip to content

Commit 290eec7

Browse files
ksundenmeeseeksmachine
authored andcommitted
Backport PR #24238: Update example and docstring to encourage the use of functools.partial in FuncAnimation
1 parent 831f145 commit 290eec7

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
@@ -1520,12 +1520,24 @@ class FuncAnimation(TimedAnimation):
15201520
func : callable
15211521
The function to call at each frame. The first argument will
15221522
be the next value in *frames*. Any additional positional
1523-
arguments can be supplied via the *fargs* parameter.
1523+
arguments can be supplied using `functools.partial` or via the *fargs*
1524+
parameter.
15241525
15251526
The required signature is::
15261527
15271528
def func(frame, *fargs) -> iterable_of_artists
15281529
1530+
It is often more convenient to provide the arguments using
1531+
`functools.partial`. In this way it is also possible to pass keyword
1532+
arguments. To pass a function with both positional and keyword
1533+
arguments, set all arguments as keyword arguments, just leaving the
1534+
*frame* argument unset::
1535+
1536+
def func(frame, art, *, y=None):
1537+
...
1538+
1539+
ani = FuncAnimation(fig, partial(func, art=ln, y='foo'))
1540+
15291541
If ``blit == True``, *func* must return an iterable of all artists
15301542
that were modified or created. This information is used by the blitting
15311543
algorithm to determine which parts of the figure have to be updated.
@@ -1564,7 +1576,8 @@ def init_func() -> iterable_of_artists
15641576
value is unused if ``blit == False`` and may be omitted in that case.
15651577
15661578
fargs : tuple or None, optional
1567-
Additional arguments to pass to each call to *func*.
1579+
Additional arguments to pass to each call to *func*. Note: the use of
1580+
`functools.partial` is preferred over *fargs*. See *func* for details.
15681581
15691582
save_count : int, default: 100
15701583
Fallback for the number of values from *frames* to cache. This is

0 commit comments

Comments
 (0)