Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8e34de

Browse files
authored
Update animated_FuncAnimation.py
1 parent 9f924fa commit c8e34de

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

galleries/examples/animation/animated_FuncAnimation.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,6 @@
99

1010
import matplotlib.animation as animation
1111

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-
3212

3313
def update(frame, line, text, decades, widgets_data):
3414
# Parameters:
@@ -51,6 +31,27 @@ def update(frame, line, text, decades, widgets_data):
5131
return line, text
5232

5333
# Set up the animation
34+
35+
# Constants
36+
decades = np.arange(1940, 2020, 10)
37+
initial_widgets = 10000 # Rough estimate of the no. of widgets in the 1950s
38+
39+
# Generate rough growth data
40+
growth_rate = np.random.uniform(1.02, 3.10, size=len(decades))
41+
widgets_data = np.cumprod(growth_rate) * initial_widgets
42+
43+
# Set up the initial plot
44+
fig, ax = plt.subplots()
45+
ax.set_xlim(1940, 2020)
46+
ax.set_ylim(0, max(widgets_data) + 100000)
47+
line, = ax.plot([], [])
48+
ax.set_xlabel('Decade')
49+
ax.set_ylabel('Number of Widgets')
50+
51+
# Text annotation to display the current decade
52+
text = ax.text(0.5, 0.85, '', transform=ax.transAxes,
53+
fontsize=12, ha='center', va='center')
54+
5455
ani = animation.FuncAnimation(
5556
fig, # Figure to update
5657
functools.partial(update, line=line, text=text, decades=decades, widgets_data=widgets_data),

0 commit comments

Comments
 (0)