Skip to content

Commit e645c62

Browse files
committed
Update ArtistAnimation example to use bar chart
1 parent b42966b commit e645c62

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def update(frame):
9090
delta = np.pi / 2
9191

9292
scat = ax.scatter(np.sin(a * t[0] + delta), np.sin(b * t[0]), c="b", s=2)
93-
ax.grid()
9493
ax.set_xlim(-1.5, 1.5)
9594
ax.set_ylim(-1.5, 1.5)
9695

@@ -152,28 +151,22 @@ def update(frame):
152151
# :class:`~matplotlib.animation.ArtistAnimation`
153152
# ----------------------------------------------
154153
#
155-
# On the other hand, :class:`~matplotlib.animation.ArtistAnimation` can be used
156-
# to generate animations if we have data on various different artists. This
157-
# list of artists is then converted frame by frame into an animation.
158-
154+
# :class:`~matplotlib.animation.ArtistAnimation` can be used
155+
# to generate animations if there is data stored on various different artists.
156+
# 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
158+
# artists for each of the bar and error bars. To update the plot, one would
159+
# need to update each of the bars from the container individually and redraw
160+
# them. Instead, `.animation.ArtistAnimation` can be used to plot each frame
161+
# individually and then stitched together to form an animation.
159162

160-
def f(x, y, mean, cov):
161-
dev_x = x - mean
162-
dev_y = y - mean
163-
maha = -0.5 * (((x-mean)/cov)**2 + ((y-mean)/cov)**2)
164-
return (1/(np.pi * cov)) * np.exp(maha)
165163

166164
fig, ax = plt.subplots()
167-
168-
x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
169-
mean = 0
170-
cov = 0.1
171-
data = f(x, y, mean, cov)
172-
aximg = ax.imshow(data)
165+
data = np.array([10, 20, 20, 30])
166+
x = [1, 2, 3, 4]
173167

174168
artists = [
175-
[ax.imshow(f(x, y, mean, 0.01 * frame + 1e-6))]
176-
for frame in range(120)
169+
list(ax.bar(x, data + i, color='b')) for i in range(10)
177170
]
178171

179172
ani = animation.ArtistAnimation(fig=fig, artists=artists, interval=100)

0 commit comments

Comments
 (0)