Skip to content

Commit

Permalink
Fix partial example
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Oct 26, 2022
1 parent cb0a6a0 commit c9b1622
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions doc/api/animation_api.rst
Expand Up @@ -108,7 +108,7 @@ this means that the callable objects you pass in must know what
artists they should be working on. There are several approaches to
handling this, of varying complexity and encapsulation. The simplest
approach, which works quite well in the case of a script, is to define the
artist at a global scope and let Python sort things out. For example ::
artist at a global scope and let Python sort things out. For example::

import numpy as np
import matplotlib.pyplot as plt
Expand All @@ -134,30 +134,29 @@ artist at a global scope and let Python sort things out. For example ::
plt.show()

The second method is to use `functools.partial` to pass arguments to the
function. ::
function::

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from functools import partial

fig, ax = plt.subplots()
line1, = plt.plot([], [], 'ro')
line1, = ax.plot([], [], 'ro')

def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
return line1,

def update(frame, ln, x, y):
x.append(frame)
y.append(np.sin(frame))
ln.set_data(xdata, ydata)
ln.set_data(x, y)
return ln,

xdata, ydata = [], []
ani = FuncAnimation(
fig, partial(update, ln=line1, x=xdata, y=ydata),
fig, partial(update, ln=line1, x=[], y=[]),
frames=np.linspace(0, 2 * np.pi, 128),
init_func=init, blit=True)

Expand Down

0 comments on commit c9b1622

Please sign in to comment.