@@ -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
138168artists and functions. A fourth method is to create a class.
139169
140170Examples
0 commit comments