-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Updates example and docstring to encourage the use of functools.partial in FuncAnimation #20358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
96d4358
e1b3df7
b739a44
7561a72
dff7c58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,7 @@ artist at a global scope and let Python sort things out. For example :: | |
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
from functools import partial | ||
|
||
fig, ax = plt.subplots() | ||
xdata, ydata = [], [] | ||
|
@@ -123,8 +124,37 @@ artist at a global scope and let Python sort things out. For example :: | |
init_func=init, blit=True) | ||
plt.show() | ||
|
||
The second method is to use `functools.partial` to 'bind' artists to | ||
function. A third method is to use closures to build up the required | ||
The second method is to use `functools.partial` to pass arguments to the | ||
function. :: | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
from functools import partial | ||
|
||
fig, ax = plt.subplots() | ||
ln, = plt.plot([], [], 'ro') | ||
|
||
def init(): | ||
ax.set_xlim(0, 2*np.pi) | ||
ax.set_ylim(-1, 1) | ||
return ln, | ||
|
||
def update(frame, x, y): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just wrong isn't it? you are appending to x and y and then setting the data with xdata and ydata. I guess I am never clear if x and y are references or not, but at the very least this is confusing... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the same, hence the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. I'd not do it this way, so whoever would do it this way should write the doc ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tacaswell Suggested the example, would this be better instead : import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from functools import partial
fig, ax = plt.subplots()
ln, = plt.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame, x_offset=0, y_offset=0):
xdata.append(frame+x_offset)
ydata.append(np.sin(frame)+y_offset)
ln.set_data(xdata, ydata)
return ln,
x_offset = 2
y_offset = 0.2
ani = FuncAnimation(
fig, partial(update, x_offset=x_offset, y_offset=y_offset),
frames=np.linspace(0, 2 * np.pi, 128),
init_func=init, blit=True)
plt.show() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the above looks better to me. But I don't understand, at all, why we wouldn't just use globals here. If an example where this is practically better than just using a global, we should write such an example. |
||
x.append(frame) | ||
y.append(np.sin(frame)) | ||
ln.set_data(xdata, ydata) | ||
return ln, | ||
|
||
xdata, ydata = [], [] | ||
ani = FuncAnimation( | ||
fig, partial(update, x=xdata, y=ydata), | ||
frames=np.linspace(0, 2 * np.pi, 128), | ||
init_func=init, blit=True) | ||
|
||
plt.show() | ||
|
||
A third method is to use closures to build up the required | ||
artists and functions. A fourth method is to create a class. | ||
|
||
Examples | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't we using partial here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I can see...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I got confused with the example below