Updates example and docstring to encourage the use of functools.partial in FuncAnimation#20358
Updates example and docstring to encourage the use of functools.partial in FuncAnimation#20358jeffreypaul15 wants to merge 5 commits intomatplotlib:mainfrom
Conversation
doc/api/animation_api.rst
Outdated
| @@ -124,7 +125,36 @@ artist at a global scope and let Python sort things out. For example :: | |||
| plt.show() | |||
|
|
|||
| The second method is to use `functools.partial` to 'bind' artists to | |||
There was a problem hiding this comment.
I think bind is doing too much work here. What about partial is used here to pass arguments to the update method - which separately is kind of confusing since this update method only takes one argument,
There was a problem hiding this comment.
So, would increasing the number of arguments be better?
There was a problem hiding this comment.
I think it could make it clearer what partial is doing here
doc/api/animation_api.rst
Outdated
| return ln, | ||
|
|
||
| ani = FuncAnimation( | ||
| fig, partial(update, offset=-0.5), |
There was a problem hiding this comment.
I guess I'm a good guinea pig for this, as I don't really know what partial is supposed to do. After this I am still mystified 😉. Why wouldn't I just pass update directly here? I don't see where offset ever gets used, so I don't see what the -0.5 means.
There was a problem hiding this comment.
Ah, my bad. I forgot to update update(), I'll make this clearer.
There was a problem hiding this comment.
This should address my issue too
There was a problem hiding this comment.
This example is also using closures (as update is closing over xdata and ydata). Maybe keep this example dropping the partial to show the closure and switch this example to something like:
def update(frame, x, y):
x.append(...)
y.append(....)
...
xdata = []
ydata = []
ani = FuncAnimation(fig, partial(update, x=xdata, y=ydata), ...)There was a problem hiding this comment.
This makes more sense, thanks for the input
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| from matplotlib.animation import FuncAnimation | ||
| from functools import partial |
There was a problem hiding this comment.
| from functools import partial |
There was a problem hiding this comment.
Aren't we using partial here?
There was a problem hiding this comment.
Sorry, I got confused with the example below
| ax.set_ylim(-1, 1) | ||
| return ln, | ||
|
|
||
| def update(frame, x, y): |
There was a problem hiding this comment.
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.
I thought the same, hence the use of offset, should I revert to that instead?
There was a problem hiding this comment.
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.
@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.
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.
|
@jeffreypaul15 what is the status of this. I've moved to draft, but feel free to move back if you think it is reviewable, or close if you don't pan to move forward. Thanks! |
|
@jeffreypaul15 Thank you for this! It ended up in #24238 with some other modifications. |
Closes #20326
Example for FuncAnimation is updated with the use of
functools.partial.A note is added in the docstring as well to use partial instead of fargs.