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

Skip to content

Commit 96d4358

Browse files
committed
Updated example and doctring
1 parent a3fbf18 commit 96d4358

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

doc/api/animation_api.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ artist at a global scope and let Python sort things out. For example ::
103103
import numpy as np
104104
import matplotlib.pyplot as plt
105105
from matplotlib.animation import FuncAnimation
106+
from functools import partial
106107

107108
fig, ax = plt.subplots()
108109
xdata, ydata = [], []
@@ -124,7 +125,35 @@ artist at a global scope and let Python sort things out. For example ::
124125
plt.show()
125126

126127
The second method is to use `functools.partial` to 'bind' artists to
127-
function. A third method is to use closures to build up the required
128+
function. ::
129+
130+
import numpy as np
131+
import matplotlib.pyplot as plt
132+
from matplotlib.animation import FuncAnimation
133+
134+
fig, ax = plt.subplots()
135+
xdata, ydata = [], []
136+
ln, = plt.plot([], [], 'ro')
137+
138+
def init():
139+
ax.set_xlim(0, 2*np.pi)
140+
ax.set_ylim(-1, 1)
141+
return ln,
142+
143+
def update(frame):
144+
xdata.append(frame)
145+
ydata.append(np.sin(frame))
146+
ln.set_data(xdata, ydata)
147+
return ln,
148+
149+
ani = FuncAnimation(
150+
fig, partial(update, offset=-0.5),
151+
frames=np.linspace(0, 2 * np.pi, 128),
152+
init_func=init, blit=True)
153+
154+
plt.show()
155+
156+
A third method is to use closures to build up the required
128157
artists and functions. A fourth method is to create a class.
129158

130159
Examples

lib/matplotlib/animation.py

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

0 commit comments

Comments
 (0)