|
46 | 46 | # *func* that modifies the data plotted on the figure. It uses the *frames*
|
47 | 47 | # parameter to determine the length of the animation. The *interval* parameter
|
48 | 48 | # 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: |
55 | 51 | #
|
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. |
82 | 64 | #
|
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 | + |
86 | 75 |
|
87 | 76 | fig, ax = plt.subplots()
|
88 | 77 | t = np.linspace(-4, 4, 400)
|
@@ -110,51 +99,14 @@ def update(frame):
|
110 | 99 | plt.show()
|
111 | 100 |
|
112 | 101 |
|
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 |
| - |
150 | 102 | ###############################################################################
|
151 | 103 | # :class:`~matplotlib.animation.ArtistAnimation`
|
152 | 104 | # ----------------------------------------------
|
153 | 105 | #
|
154 | 106 | # :class:`~matplotlib.animation.ArtistAnimation` can be used
|
155 | 107 | # to generate animations if there is data stored on various different artists.
|
156 | 108 | # 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 |
158 | 110 | # artists for each of the bar and error bars. To update the plot, one would
|
159 | 111 | # need to update each of the bars from the container individually and redraw
|
160 | 112 | # them. Instead, `.animation.ArtistAnimation` can be used to plot each frame
|
|
0 commit comments