Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66dd732

Browse filesBrowse files
authored
Merge pull request #24918 from jklymak/doc-shorten-animation
DOC: animation faster
2 parents b603ab6 + c14227b commit 66dd732
Copy full SHA for 66dd732

File tree

1 file changed

+21
-16
lines changed
Filter options

1 file changed

+21
-16
lines changed

‎tutorials/introductory/animation_tutorial.py

Copy file name to clipboardExpand all lines: tutorials/introductory/animation_tutorial.py
+21-16Lines changed: 21 additions & 16 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.