7
7
histogram.
8
8
"""
9
9
10
+ import functools
11
+
10
12
import matplotlib .pyplot as plt
11
13
import numpy as np
12
14
24
26
# %%
25
27
# To animate the histogram, we need an ``animate`` function, which generates
26
28
# a random set of numbers and updates the heights of rectangles. The ``animate``
27
- # function updates the `.Rectangle` patches on a global instance of
28
- # `.BarContainer` (which in this case is defined below).
29
+ # function updates the `.Rectangle` patches on an instance of `.BarContainer`.
29
30
30
31
31
- def animate (frame_number ):
32
+ def animate (frame_number , bar_container ):
32
33
# Simulate new data coming in.
33
34
data = rng .standard_normal (1000 )
34
35
n , _ = np .histogram (data , HIST_BINS )
@@ -39,7 +40,9 @@ def animate(frame_number):
39
40
40
41
# %%
41
42
# Using :func:`~matplotlib.pyplot.hist` allows us to get an instance of
42
- # `.BarContainer`, which is a collection of `.Rectangle` instances.
43
+ # `.BarContainer`, which is a collection of `.Rectangle` instances. Since
44
+ # `.FuncAnimation` will only pass the frame number parameter to the animation
45
+ # function, we use `functools.partial` to fix the ``bar_container``` parameter.
43
46
44
47
# Output generated via `matplotlib.animation.Animation.to_jshtml`.
45
48
@@ -48,7 +51,8 @@ def animate(frame_number):
48
51
ec = "yellow" , fc = "green" , alpha = 0.5 )
49
52
ax .set_ylim (top = 55 ) # set safe limit to ensure that all data is visible.
50
53
51
- ani = animation .FuncAnimation (fig , animate , 50 , repeat = False , blit = True )
54
+ anim = functools .partial (animate , bar_container = bar_container )
55
+ ani = animation .FuncAnimation (fig , anim , 50 , repeat = False , blit = True )
52
56
plt .show ()
53
57
54
58
# %%
0 commit comments