Skip to content

Commit d05eaed

Browse files
committed
Barchart race example for ArtistAnimation
1 parent e645c62 commit d05eaed

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tutorials/introductory/animation_tutorial.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,25 @@ def update(frame):
158158
# artists for each of the bar and error bars. To update the plot, one would
159159
# need to update each of the bars from the container individually and redraw
160160
# them. Instead, `.animation.ArtistAnimation` can be used to plot each frame
161-
# individually and then stitched together to form an animation.
161+
# individually and then stitched together to form an animation. A barchart race
162+
# is a simple example for this.
162163

163164

164165
fig, ax = plt.subplots()
165-
data = np.array([10, 20, 20, 30])
166-
x = [1, 2, 3, 4]
166+
rng = np.random.default_rng()
167+
data = np.array([20, 20, 20, 20])
168+
x = np.array([1, 2, 3, 4])
169+
170+
artists = []
171+
colors = {1: 'b', 2: 'r', 3: 'k', 4: 'g'}
172+
for i in range(20):
173+
data += rng.integers(low=0, high=10, size=data.shape)
174+
order = data.argsort()
175+
container = ax.barh(x, data[order], color=[colors[x[o]] for o in order])
176+
artists.append(container)
167177

168-
artists = [
169-
list(ax.bar(x, data + i, color='b')) for i in range(10)
170-
]
171178

172-
ani = animation.ArtistAnimation(fig=fig, artists=artists, interval=100)
179+
ani = animation.ArtistAnimation(fig=fig, artists=artists, interval=400)
173180
plt.show()
174181

175182
###############################################################################

0 commit comments

Comments
 (0)