| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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 = [], [] | ||
| Expand All | @@ -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): | ||
|
Comment thread
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis 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...
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI thought the same, hence the use of offset, should I revert to that instead?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI don't know. I'd not do it this way, so whoever would do it this way should write the doc ;-)
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality@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()
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI 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.
Sorry, something went wrong.
jeffreypaul15 reacted with thumbs up emoji
All reactions
|
||
| 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 | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySorry, something went wrong.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAren't we using partial here?
Sorry, something went wrong.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNot that I can see...
Sorry, something went wrong.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySorry, I got confused with the example below
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.