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

Skip to content

Commit 56192f1

Browse files
jeffreypaul15oscargus
authored andcommitted
Update example and docstring to encourage the use of functools.partial
1 parent 231d1c8 commit 56192f1

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

doc/api/animation_api.rst

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ artist at a global scope and let Python sort things out. For example ::
113113
import numpy as np
114114
import matplotlib.pyplot as plt
115115
from matplotlib.animation import FuncAnimation
116+
from functools import partial
116117

117118
fig, ax = plt.subplots()
118119
xdata, ydata = [], []
@@ -133,8 +134,37 @@ artist at a global scope and let Python sort things out. For example ::
133134
init_func=init, blit=True)
134135
plt.show()
135136

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

140170
Examples

lib/matplotlib/animation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,8 @@ def init_func() -> iterable_of_artists
15641564
value is unused if ``blit == False`` and may be omitted in that case.
15651565
15661566
fargs : tuple or None, optional
1567-
Additional arguments to pass to each call to *func*.
1567+
Additional arguments to pass to each call to *func*. Note: the use of
1568+
`functools.partial` is preferred over *fargs*.
15681569
15691570
save_count : int, default: 100
15701571
Fallback for the number of values from *frames* to cache. This is

0 commit comments

Comments
 (0)