-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass_map.py
87 lines (66 loc) · 3.25 KB
/
pass_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import matplotlib.pyplot as plt
import pandas as pd
from mplsoccer import Pitch, Sbopen
from statsbombpy import sb
def draw_player_pass_to_shot(passes, matches_dict):
pitch = Pitch(line_color='white', pitch_color='#02540b', pad_top=20)
fig, ax = pitch.draw(figsize=(10, 7))
for i, thepass in passes.iterrows():
x = thepass['location'][0]
y = thepass['location'][1]
# plot circle
passCircle = plt.Circle((x, y), 2, color="white")
passCircle.set_alpha(.2)
ax.add_patch(passCircle)
dx = thepass['pass_end_location'][0] - x
dy = thepass['pass_end_location'][1] - y
# plot arrow
if not pd.isna(thepass['pass_goal_assist']):
passArrow = plt.Arrow(x, y, dx, dy, width=3, color="red")
ax.add_patch(passArrow)
elif not pd.isna(thepass['pass_assisted_shot_id']):
passArrow = plt.Arrow(x, y, dx, dy, width=2, color="black")
ax.add_patch(passArrow)
elif thepass['pass_outcome'] != 'Incomplete':
passArrow = plt.Arrow(x, y, dx, dy, width=1.2, color="wheat")
ax.add_patch(passArrow)
# else:
# passArrow = plt.Arrow(x, y, dx, dy, width=1, color="blue")
# ax.add_patch(passArrow)
# plt.annotate(thepass['shot_statsbomb_xg'], xy=(x, y))
ax.set_title("from arda passes to", fontsize=24)
fig.set_size_inches(10, 7)
plt.show()
# all_matches_seperate_pass_map(passes)
# passes_separately(passes, matches_dict)
def all_matches_seperate_pass_map(events):
# pitch = all_passes(events)
# passes_seperately(events, pitch)
passes_separately(events)
def passes_separately(passes, matches_dict):
pitch = Pitch(line_color='white', pitch_color='#02540b', pad_top=20)
fig, axs = pitch.grid(ncols=2, nrows=3, grid_height=0.85, title_height=0.06, axis=False,
endnote_height=0.04, title_space=0.04, endnote_space=0.01)
match_ids = passes['match_id'].unique()
for match, ax in zip(match_ids, axs['pitch'].flat[:len(match_ids)]):
# put match_id over the plot
ax.text(60, -10, matches_dict.get(match),
ha='center', va='center', fontsize=8)
# take only passes by this player, for now its already only Neymar's passes.
player_df = passes.loc[passes["match_id"] == match]
# scatter
pitch.scatter(player_df.x, player_df.y, alpha=0.2, s=50, color="blue", ax=ax)
# plot arrow
goal_df = player_df.loc[player_df["pass_outcome"] != 'Incomplete']
pitch.arrows(goal_df.x, goal_df.y,
goal_df.end_x, goal_df.end_y, color="yellow", ax=ax, width=1)
non_goal_df = player_df.loc[player_df["pass_outcome"] == 'Incomplete']
pitch.arrows(non_goal_df.x, non_goal_df.y,
non_goal_df.end_x, non_goal_df.end_y, color="red", ax=ax, width=1)
# plt.annotate(player_df['shot_statsbomb_xg'], xy=(player_df.x, player_df.y))
# We have more than enough pitches - remove them
for ax in axs['pitch'][-1, 16 - len(match_ids):]:
ax.remove()
# Another way to set title using mplsoccer
axs['title'].text(0.5, 0.5, 'Neymar passes converted to shot, every game', ha='center', va='center', fontsize=24)
plt.show()