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

Skip to content

Commit ffac8a1

Browse files
committed
updated passing_args_guide
1 parent af80ab1 commit ffac8a1

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
plt.rcParams["backend"] = "TKAgg"
5+
plt.ion()
6+
fig, ax = plt.subplots()
7+
8+
data = np.arange(6).reshape(2, 3)
9+
10+
qm = ax.pcolormesh(
11+
["col 1", "col 2", "col 3"],
12+
["row 1", "row 2"],
13+
data,
14+
)
15+
16+
17+
def on_mouse_move(event):
18+
if not event.inaxes:
19+
return
20+
cd = qm.get_cursor_data(event)
21+
if cd is None:
22+
return
23+
24+
# The value of the cell, according to get_cursor_data()
25+
cd_val = cd[0]
26+
27+
# The value of the cell, according to the data row/col
28+
data_val = data[round(event.ydata), round(event.xdata)]
29+
30+
if cd_val == data_val:
31+
print("Fine")
32+
else:
33+
print(event)
34+
print(cd)
35+
raise ValueError(f"Value mismatch. {cd_val} != {data_val}")
36+
37+
38+
fig.canvas.mpl_connect("motion_notify_event", on_mouse_move)
39+
fig.canvas.manager.window.geometry("800x1054+100+100")

galleries/examples/animation/animated_FuncAnimation.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
"""
2-
==================
2+
==================================
33
Parameterized Animation Functions
4-
==================
4+
==================================
55
use FuncAnimation to show counts of widgets over the decades
66
"""
7+
import functools
8+
79
import matplotlib.pyplot as plt
810
import numpy as np
911

1012
import matplotlib.animation as animation
1113

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

3315
def update(frame, line, text, decades, widgets_data):
34-
# Update function for FuncAnimation.
35-
3616
# Parameters:
3717
# frame (int): The current frame number.
3818
# line (Line2D): The line object to update.
@@ -53,11 +33,32 @@ def update(frame, line, text, decades, widgets_data):
5333
return line, text
5434

5535
# 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+
5657
ani = animation.FuncAnimation(
5758
fig, # Figure to update
58-
update, # Update function
59+
functools.partial(update, line=line, text=text,
60+
decades=decades, widgets_data=widgets_data),
5961
frames=len(decades), # Number of frames
60-
fargs=(line, text, decades, widgets_data), # Additional arguments for update
6162
interval=1000, # Delay between frames in milliseconds
6263
blit=False, # Whether to use blit for faster updates
6364
)

0 commit comments

Comments
 (0)