1
1
"""
2
- ==================
2
+ ==================================
3
3
Parameterized Animation Functions
4
- ==================
4
+ ==================================
5
5
use FuncAnimation to show counts of widgets over the decades
6
6
"""
7
+ import functools
8
+
7
9
import matplotlib .pyplot as plt
8
10
import numpy as np
9
11
10
12
import matplotlib .animation as animation
11
13
12
- # Constants
13
- decades = np .arange (1940 , 2020 , 10 )
14
- initial_widgets = 10000 # Rough estimate of the no. of widgets in the 1950s
15
-
16
- # Generate rough growth data
17
- growth_rate = np .random .uniform (1.02 , 3.10 , size = len (decades ))
18
- widgets_data = np .cumprod (growth_rate ) * initial_widgets
19
-
20
- # Set up the initial plot
21
- fig , ax = plt .subplots ()
22
- ax .set_xlim (1940 , 2020 )
23
- ax .set_ylim (0 , max (widgets_data ) + 100000 )
24
- line , = ax .plot ([], [])
25
- ax .set_xlabel ('Decade' )
26
- ax .set_ylabel ('Number of Widgets' )
27
-
28
- # Text annotation to display the current decade
29
- text = ax .text (0.5 , 0.85 , '' , transform = ax .transAxes ,
30
- fontsize = 12 , ha = 'center' , va = 'center' )
31
-
32
14
33
15
def update (frame , line , text , decades , widgets_data ):
34
- # Update function for FuncAnimation.
35
-
36
16
# Parameters:
37
17
# frame (int): The current frame number.
38
18
# line (Line2D): The line object to update.
@@ -53,11 +33,32 @@ def update(frame, line, text, decades, widgets_data):
53
33
return line , text
54
34
55
35
# Set up the animation
36
+
37
+ # Constants
38
+ decades = np .arange (1940 , 2020 , 10 )
39
+ initial_widgets = 10000 # Rough estimate of the no. of widgets in the 1950s
40
+
41
+ # Generate rough growth data
42
+ growth_rate = np .random .uniform (1.02 , 3.10 , size = len (decades ))
43
+ widgets_data = np .cumprod (growth_rate ) * initial_widgets
44
+
45
+ # Set up the initial plot
46
+ fig , ax = plt .subplots ()
47
+ ax .set_xlim (1940 , 2020 )
48
+ ax .set_ylim (0 , max (widgets_data ) + 100000 )
49
+ line , = ax .plot ([], [])
50
+ ax .set_xlabel ('Decade' )
51
+ ax .set_ylabel ('Number of Widgets' )
52
+
53
+ # Text annotation to display the current decade
54
+ text = ax .text (0.5 , 0.85 , '' , transform = ax .transAxes ,
55
+ fontsize = 12 , ha = 'center' , va = 'center' )
56
+
56
57
ani = animation .FuncAnimation (
57
58
fig , # Figure to update
58
- update , # Update function
59
+ functools .partial (update , line = line , text = text ,
60
+ decades = decades , widgets_data = widgets_data ),
59
61
frames = len (decades ), # Number of frames
60
- fargs = (line , text , decades , widgets_data ), # Additional arguments for update
61
62
interval = 1000 , # Delay between frames in milliseconds
62
63
blit = False , # Whether to use blit for faster updates
63
64
)
0 commit comments