Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: animation faster #24918

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

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions tutorials/introductory/animation_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,37 @@
#
# Covering the set methods for all types of artists is beyond the scope of this
# tutorial but can be found in their respective documentations. An example of
# such update methods in use for `.Axes.scatter` is as follows.

# such update methods in use for `.Axes.scatter` and `.Axes.plot` is as follows.

fig, ax = plt.subplots()
t = np.linspace(-np.pi, np.pi, 400)
a, b = 3, 2
delta = np.pi / 2
t = np.linspace(0, 3, 40)
g = -9.81
v0 = 12
z = g * t**2 / 2 + v0 * t

v02 = 5
z2 = g * t**2 / 2 + v02 * t

scat = ax.scatter(np.sin(a * t[0] + delta), np.sin(b * t[0]), c="b", s=2)
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s')
line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0]
ax.set(xlim=[0, 3], ylim=[-4, 10], xlabel='Time [s]', ylabel='Z [m]')
ax.legend()


def update(frame):
# .set_offsets replaces the offset data for the entire collection with
# the new values. Therefore, to also carry forward the previously
# calculated information, we use the data from the first to the current
# frame to set the new offsets.
Comment on lines -104 to -107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like an explanation about why we take the entire data for the new frame should be there since this is a tutorial. Maybe reword it a little but not remove it completely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it a bit. Note I also added comments for the steps where the data was added, which basically says what you are saying in the old text, so I think it's relatively clear? If not, happy to take suggestions.

x = np.sin(a * t[:frame] + delta)
y = np.sin(b * t[:frame])
# for each frame, update the data stored on each artist.
x = t[:frame]
y = z[:frame]
# update the scatter plot:
data = np.stack([x, y]).T
scat.set_offsets(data)
return (scat,)
# update the line plot:
line2.set_xdata(t[:frame])
line2.set_ydata(z2[:frame])
return (scat, line2)


ani = animation.FuncAnimation(fig=fig, func=update, frames=400, interval=30)
ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
plt.show()


Expand Down