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

Skip to content

Commit 449a57c

Browse files
committed
DOC: change artists, add annotation
1 parent ba51c88 commit 449a57c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,36 @@
8989
# tutorial but can be found in their respective documentations. An example of
9090
# such update methods in use for `.Axes.scatter` is as follows.
9191

92-
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):
104108
# .set_offsets replaces the offset data for the entire collection with
105109
# the new values. Therefore, to also carry forward the previously
106110
# calculated information, we use the data from the first to the current
107111
# frame to set the new offsets.
108-
x = np.sin(a * t[:frame] + delta)
109-
y = np.sin(b * t[:frame])
112+
x = t[:frame]
113+
y = z[:frame]
110114
data = np.stack([x, y]).T
111115
scat.set_offsets(data)
112-
return (scat,)
116+
line2.set_xdata(t[:frame])
117+
line2.set_ydata(z2[:frame])
118+
return (scat, line2)
113119

114120

115-
ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=300)
121+
ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
116122
plt.show()
117123

118124

0 commit comments

Comments
 (0)