@@ -158,18 +158,25 @@ def update(frame):
158
158
# artists for each of the bar and error bars. To update the plot, one would
159
159
# need to update each of the bars from the container individually and redraw
160
160
# 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.
162
163
163
164
164
165
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 )
167
177
168
- artists = [
169
- list (ax .bar (x , data + i , color = 'b' )) for i in range (10 )
170
- ]
171
178
172
- ani = animation .ArtistAnimation (fig = fig , artists = artists , interval = 100 )
179
+ ani = animation .ArtistAnimation (fig = fig , artists = artists , interval = 400 )
173
180
plt .show ()
174
181
175
182
###############################################################################
0 commit comments