Skip to content

Commit 9a0e85e

Browse files
committed
Fix example for line animation
The animation for Lines2D was not showing the animation properly but was showing just the final frame. Adding an init function resolved this issue and the animation appears correctly now.
1 parent a15d212 commit 9a0e85e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
# The process of animation can be thought about in 2 different ways:
2222
#
2323
# - :class:`~matplotlib.animation.FuncAnimation`: Generate data for first
24-
# frame and then modify this data for each frame to create an animated plot.
24+
# frame and then modify this data for each frame to create an animated plot.
2525
#
2626
# - :class:`~matplotlib.animation.FuncAnimation`: Generate a list (iterable)
27-
# of artists that will draw in each frame in the animation.
27+
# of artists that will draw in each frame in the animation.
2828
#
2929
# :class:`~matplotlib.animation.FuncAnimation` is more efficient in terms of
3030
# speed and memory as it draws an artist once and then modifies it. On the
@@ -63,8 +63,15 @@
6363
xdata, ydata = [], []
6464
(line,) = ax.plot(xdata, ydata, c="b")
6565
ax.grid()
66-
ax.set_ylim(-1, 1)
67-
ax.set_xlim(0, 10)
66+
67+
68+
def init():
69+
ax.set_ylim(-1.1, 1.1)
70+
ax.set_xlim(0, 5)
71+
del xdata[:]
72+
del ydata[:]
73+
line.set_data(xdata, ydata)
74+
return line,
6875

6976

7077
def update(frame):
@@ -77,7 +84,8 @@ def update(frame):
7784
return (line,)
7885

7986

80-
ani = animation.FuncAnimation(fig=fig, func=update, interval=30)
87+
ani = animation.FuncAnimation(fig=fig, func=update,
88+
interval=30, init_func=init)
8189
plt.show()
8290

8391
###############################################################################
@@ -90,7 +98,7 @@ def update(frame):
9098

9199
fig, ax = plt.subplots()
92100
rng = np.random.default_rng()
93-
t = np.linspace(-4, 4, 1000)
101+
t = np.linspace(-4, 4, 400)
94102
a, b = 3, 2
95103
delta = np.pi / 2
96104

@@ -110,7 +118,7 @@ def update(frame):
110118
return (scat,)
111119

112120

113-
ani = animation.FuncAnimation(fig=fig, func=update, interval=30)
121+
ani = animation.FuncAnimation(fig=fig, func=update, frames=400, interval=30)
114122
plt.show()
115123

116124

@@ -169,21 +177,21 @@ def update(frame):
169177
# by all writers. There are 4 major types of writers:
170178
#
171179
# - :class:`~matplotlib.animation.PillowWriter` - Uses the Pillow library to
172-
# create the animation.
180+
# create the animation.
173181
#
174182
# - :class:`~matplotlib.animation.HTMLWriter` - Used to create JS-based
175-
# animations.
183+
# animations.
176184
#
177185
# - Pipe-based writers - :class:`~matplotlib.animation.FFMpegWriter` and
178-
# :class:`~matplotlib.animation.ImageMagickWriter` are pipe based writers.
179-
# These writers pipe each frame to the utility (*ffmpeg* / *imagemagick*) which
180-
# then stitches all of them together to create the animation.
186+
# :class:`~matplotlib.animation.ImageMagickWriter` are pipe based writers.
187+
# These writers pipe each frame to the utility (*ffmpeg* / *imagemagick*)
188+
# which then stitches all of them together to create the animation.
181189
#
182190
# - File-based writers - :class:`~matplotlib.animation.FFMpegFileWriter` and
183-
# :class:`~matplotlib.animation.ImageMagickFileWriter` are examples of
184-
# file-based writers. These writers are slower than their standard writers but
185-
# are more useful for debugging as they save each frame in a file before
186-
# stitching them together into an animation.
191+
# :class:`~matplotlib.animation.ImageMagickFileWriter` are examples of
192+
# file-based writers. These writers are slower than their standard writers
193+
# but are more useful for debugging as they save each frame in a file before
194+
# stitching them together into an animation.
187195
#
188196
# ================================================ ===========================
189197
# Writer Supported Formats

0 commit comments

Comments
 (0)