Skip to content

Commit b64f490

Browse files
authored
Merge pull request #24949 from meeseeksmachine/auto-backport-of-pr-24918-on-v3.7.x
Backport PR #24918 on branch v3.7.x (DOC: animation faster)
2 parents 45d2106 + 53e3c72 commit b64f490

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

tutorials/introductory/animation_tutorial.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,37 @@
8787
#
8888
# Covering the set methods for all types of artists is beyond the scope of this
8989
# tutorial but can be found in their respective documentations. An example of
90-
# such update methods in use for `.Axes.scatter` is as follows.
91-
90+
# such update methods in use for `.Axes.scatter` and `.Axes.plot` is as follows.
9291

9392
fig, ax = plt.subplots()
94-
t = np.linspace(-np.pi, np.pi, 400)
95-
a, b = 3, 2
96-
delta = np.pi / 2
93+
t = np.linspace(0, 3, 40)
94+
g = -9.81
95+
v0 = 12
96+
z = g * t**2 / 2 + v0 * t
97+
98+
v02 = 5
99+
z2 = g * t**2 / 2 + v02 * t
97100

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

102106

103107
def update(frame):
104-
# .set_offsets replaces the offset data for the entire collection with
105-
# the new values. Therefore, to also carry forward the previously
106-
# calculated information, we use the data from the first to the current
107-
# frame to set the new offsets.
108-
x = np.sin(a * t[:frame] + delta)
109-
y = np.sin(b * t[:frame])
108+
# for each frame, update the data stored on each artist.
109+
x = t[:frame]
110+
y = z[:frame]
111+
# update the scatter plot:
110112
data = np.stack([x, y]).T
111113
scat.set_offsets(data)
112-
return (scat,)
114+
# update the line plot:
115+
line2.set_xdata(t[:frame])
116+
line2.set_ydata(z2[:frame])
117+
return (scat, line2)
113118

114119

115-
ani = animation.FuncAnimation(fig=fig, func=update, frames=400, interval=30)
120+
ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
116121
plt.show()
117122

118123

0 commit comments

Comments
 (0)