21
21
# The process of animation can be thought about in 2 different ways:
22
22
#
23
23
# - :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.
25
25
#
26
26
# - :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.
28
28
#
29
29
# :class:`~matplotlib.animation.FuncAnimation` is more efficient in terms of
30
30
# speed and memory as it draws an artist once and then modifies it. On the
63
63
xdata , ydata = [], []
64
64
(line ,) = ax .plot (xdata , ydata , c = "b" )
65
65
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 ,
68
75
69
76
70
77
def update (frame ):
@@ -77,7 +84,8 @@ def update(frame):
77
84
return (line ,)
78
85
79
86
80
- ani = animation .FuncAnimation (fig = fig , func = update , interval = 30 )
87
+ ani = animation .FuncAnimation (fig = fig , func = update ,
88
+ interval = 30 , init_func = init )
81
89
plt .show ()
82
90
83
91
###############################################################################
@@ -90,7 +98,7 @@ def update(frame):
90
98
91
99
fig , ax = plt .subplots ()
92
100
rng = np .random .default_rng ()
93
- t = np .linspace (- 4 , 4 , 1000 )
101
+ t = np .linspace (- 4 , 4 , 400 )
94
102
a , b = 3 , 2
95
103
delta = np .pi / 2
96
104
@@ -110,7 +118,7 @@ def update(frame):
110
118
return (scat ,)
111
119
112
120
113
- ani = animation .FuncAnimation (fig = fig , func = update , interval = 30 )
121
+ ani = animation .FuncAnimation (fig = fig , func = update , frames = 400 , interval = 30 )
114
122
plt .show ()
115
123
116
124
@@ -169,21 +177,21 @@ def update(frame):
169
177
# by all writers. There are 4 major types of writers:
170
178
#
171
179
# - :class:`~matplotlib.animation.PillowWriter` - Uses the Pillow library to
172
- # create the animation.
180
+ # create the animation.
173
181
#
174
182
# - :class:`~matplotlib.animation.HTMLWriter` - Used to create JS-based
175
- # animations.
183
+ # animations.
176
184
#
177
185
# - 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.
181
189
#
182
190
# - 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.
187
195
#
188
196
# ================================================ ===========================
189
197
# Writer Supported Formats
0 commit comments