@@ -108,7 +108,7 @@ this means that the callable objects you pass in must know what
108108artists they should be working on. There are several approaches to
109109handling this, of varying complexity and encapsulation. The simplest
110110approach, 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
138166artists and functions. A fourth method is to create a class.
139167
140168Examples
0 commit comments