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
13
15
import matplotlib .animation as animation
14
16
15
- # Fixing random state for reproducibility
16
- np .random .seed ( 19680801 )
17
- # Fixing bin edges
17
+ # Setting up a random number generator with a fixed state for reproducibility.
18
+ rng = np .random .default_rng ( seed = 19680801 )
19
+ # Fixing bin edges.
18
20
HIST_BINS = np .linspace (- 4 , 4 , 100 )
19
21
20
- # histogram our data with numpy
21
- data = np . random . randn (1000 )
22
+ # Histogram our data with numpy.
23
+ data = rng . standard_normal (1000 )
22
24
n , _ = np .histogram (data , HIST_BINS )
23
25
24
26
# %%
25
27
# To animate the histogram, we need an ``animate`` function, which generates
26
- # a random set of numbers and updates the heights of rectangles. We utilize a
27
- # python closure to track an instance of `.BarContainer` whose `.Rectangle`
28
- # patches we shall update.
28
+ # a random set of numbers and updates the heights of rectangles. The ``animate``
29
+ # function updates the `.Rectangle` patches on an instance of `.BarContainer`.
29
30
30
31
31
- def prepare_animation (bar_container ):
32
+ def animate (frame_number , bar_container ):
33
+ # Simulate new data coming in.
34
+ data = rng .standard_normal (1000 )
35
+ n , _ = np .histogram (data , HIST_BINS )
36
+ for count , rect in zip (n , bar_container .patches ):
37
+ rect .set_height (count )
32
38
33
- def animate (frame_number ):
34
- # simulate new data coming in
35
- data = np .random .randn (1000 )
36
- n , _ = np .histogram (data , HIST_BINS )
37
- for count , rect in zip (n , bar_container .patches ):
38
- rect .set_height (count )
39
- return bar_container .patches
40
- return animate
39
+ return bar_container .patches
41
40
42
41
# %%
43
42
# Using :func:`~matplotlib.pyplot.hist` allows us to get an instance of
44
- # `.BarContainer`, which is a collection of `.Rectangle` instances. Calling
45
- # ``prepare_animation`` will define ``animate`` function working with supplied
46
- # `.BarContainer`, all this is used to setup `.FuncAnimation` .
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 .
47
46
48
47
# Output generated via `matplotlib.animation.Animation.to_jshtml`.
49
48
@@ -52,6 +51,6 @@ def animate(frame_number):
52
51
ec = "yellow" , fc = "green" , alpha = 0.5 )
53
52
ax .set_ylim (top = 55 ) # set safe limit to ensure that all data is visible.
54
53
55
- ani = animation . FuncAnimation ( fig , prepare_animation ( bar_container ), 50 ,
56
- repeat = False , blit = True )
54
+ anim = functools . partial ( animate , bar_container = bar_container )
55
+ ani = animation . FuncAnimation ( fig , anim , 50 , repeat = False , blit = True )
57
56
plt .show ()
0 commit comments