8000 Change FuncAnimation to have one general guideline · chahak13/matplotlib@46e691d · GitHub
[go: up one dir, main page]

Skip to content

Commit 46e691d

Browse files
committed
Change FuncAnimation to have one general guideline
1 parent d05eaed commit 46e691d

File tree

1 file changed

+25
-73
lines changed

1 file changed

+25
-73
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,32 @@
4646
# *func* that modifies the data plotted on the figure. It uses the *frames*
4747
# parameter to determine the length of the animation. The *interval* parameter
4848
# is used to determine time in milliseconds between drawing of two frames.
49-
# We will now look at some examples of using
50-
# :class:`~matplotlib.animation.FuncAnimation` with different artists.
51-
52-
###############################################################################
53-
# Animating Lines
54-
# ^^^^^^^^^^^^^^^
49+
# Animating using `.FuncAnimation` would usually follow the following
50+
# structure:
5551
#
56-
# `.pyplot.plot` returns a :class:`~matplotlib.lines.Line2D` collection. The
57-
# data on this collection can be modified by using the
58-
# `.lines.Line2D.set_data` function. Therefore we can use this to modify the
59-
# plot using the function for every frame.
60-
61-
fig, ax = plt.subplots()
62-
63-
xdata = np.arange(0, 2 * np.pi, 0.01)
64-
(line,) = ax.plot(xdata, np.sin(xdata), c="b")
65-
ax.set_ylim(-1.1, 1.1)
66-
67-
68-
def update(frame):
69-
# .set_ydata resets the y-data for the line, so we add the new point to
70-
# the existing line x-data and calculate y again.
71-
line.set_ydata(np.sin(xdata + frame / 50))
72-
return (line,)
73-
74-
75-
ani = animation.FuncAnimation(fig=fig, func=update,
76-
interval=30)
77-
plt.show()
78-
79-
###############################################################################
80-
# Animating Markers
81-
# ^^^^^^^^^^^^^^^^^
52+
# - Plot the initial figure, including all the required artists. Save all the
53+
# artists in variables so that they can be updated later on during the
54+
# animation.
55+
# - Create an animation function that updates the data in each artist to
56+
# generate the new frame at each function call.
57+
# - Create a `.FuncAnimation` object with the `.Figure` and the animation
58+
# function, along with the keyword arguments.
59+
# - Use `.animation.Animation.save` or `.pyplot.show` to save or show the
60+
# animation.
61+
#
62+
# The update function uses the `set_*` function for different artists to modify
63+
# the data.
8264
#
83-
# `.pyplot.scatter` returns a :class:`~matplotlib.collections.PathCollection`
84-
# that can similarly be modified by using the
85-
# `.collections.PathCollection.set_offsets` function.
65+
# ============================= =========================================
66+
# Artist Set method
67+
# ============================= =========================================
68+
# `.lines.Line2D` `.lines.Line2D.set_data`
69+
# `.collections.PathCollection` `.collections.PathCollection.set_offsets`
70+
# `.image.AxesImage` `.image.AxesImage.set_data`
71+
# ============================= =========================================
72+
#
73+
# An example for animating a `.Axes.scatter` plot is
74+
8675

8776
fig, ax = plt.subplots()
8877
t = np.linspace(-4, 4, 400)
@@ -110,51 +99,14 @@ def update(frame):
11099
plt.show()
111100

112101

113-
###############################################################################
114-
# Animating Images
115-
# ^^^^^^^^^^^^^^^^
116-
#
117-
# When we plot an image using `.pyplot.imshow`, it returns an
118-
# :class:`~matplotlib.image.AxesImage` object. The data in this object can also
119-
# similarly be modified by using the `.image.AxesImage.set_data` method.
120-
121-
122-
def f(x, y, mean, cov):
123-
dev_x = x - mean
124-
dev_y = y - mean
125-
maha = -0.5 * (((x-mean)/cov)**2 + ((y-mean)/cov)**2)
126-
return (1/(np.pi * cov)) * np.exp(maha)
127-
128-
fig, ax = plt.subplots()
129-
130-
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
131-
mean = 0
132-
cov = 0.1
133-
data = f(x, y, mean, cov)
134-
aximg = ax.imshow(data)
135-
136-
137-
def update(frame):
138-
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
139-
mean = 0
140-
cov = 0.01 * frame + 1e-6
141-
data = f(x, y, mean, cov)
142-
143-
aximg.set_data(data)
144-
return (aximg,)
145-
146-
147-
ani = animation.FuncAnimation(fig=fig, func=update, frames=None, interval=100)
148-
plt.show()
149-
150102
###############################################################################
151103
# :class:`~matplotlib.animation.ArtistAnimation`
152104
# ----------------------------------------------
153105
#
154106
# :class:`~matplotlib.animation.ArtistAnimation` can be used
155107
# to generate animations if there is data stored on various different artists.
156108
# This list of artists is then converted frame by frame into an animation. For
157-
# example, when we use `Axes.bar` to plot a bar-chart, it creates a number of
109+
# example, when we use `.Axes.barh` to plot a bar-chart, it creates a number of
158110
# artists for each of the bar and error bars. To update the plot, one would
159111
# need to update each of the bars from the container individually and redraw
160112
# them. Instead, `.animation.ArtistAnimation` can be used to plot each frame

0 commit comments

Comments
 (0)
0