-
Notifications
You must be signed in to change notification settings - Fork 0
/
passes_to_area.py
131 lines (100 loc) · 4.97 KB
/
passes_to_area.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import pandas as pd
from statsbombpy import sb
import matplotlib.pyplot as plt
from mplsoccer import Pitch
import numpy as np
import streamlit as st
from matplotlib import cm
def get_passes(match_id_list, player_name):
# print(player_name)
# print(match_id_list)
all_passes = pd.DataFrame()
for match in match_id_list:
events = sb.events(match_id=match)
# Filter for passes made by the player
passes = events[(events['type'] == 'Pass') & (events['player'] == player_name)]
# Filter for successful passes
successful_passes = passes[passes['pass_outcome'].isna()]
all_passes = pd.concat([all_passes, successful_passes])
# match_passes_dict[match] = all_passes
calculate_area_percentages(all_passes, player_name)
def calculate_area_percentages(all_passes, player_name):
# Divide the pitch into 12 equal areas (3 vertical x 4 horizontal)
pitch_length = 120
pitch_width = 80
# Extract end locations of the passes
pass_end_location = pd.DataFrame(all_passes['pass_end_location'].tolist(), index=all_passes.index,
columns=['x_end', 'y_end'])
all_passes = pd.concat([all_passes, pass_end_location], axis=1)
# Determine which area each pass ends in
bins_x = np.linspace(0, pitch_length, 4) # 3 vertical divisions
bins_y = np.linspace(0, pitch_width, 5) # 4 horizontal divisions
all_passes['area'] = pd.cut(all_passes['x_end'], bins_x, labels=False, include_lowest=True) + \
pd.cut(all_passes['y_end'], bins_y, labels=False, include_lowest=True) * 3
# Calculate the percentage of passes in each area
# area_counts = passes['area'].value_counts(normalize=True).sort_index() * 100
area_counts = all_passes['area'].value_counts().sort_index()
total_passes = area_counts.sum()
area_percentages = area_counts / total_passes * 100
plot_area_percentages(area_counts, area_percentages, bins_x, bins_y, player_name)
def plot_area_percentages(area_counts, area_percentages, bins_x, bins_y, player_name):
# Create a football pitch
pitch = Pitch(pitch_type='statsbomb', pitch_color='grass', line_color='white', stripe=False)
fig, ax = pitch.draw(figsize=(12, 8))
norm = plt.Normalize(area_percentages.min(), area_percentages.max())
# Create a colormap (single color gradient)
cmap = cm.coolwarm
# Sort areas by percentage
sorted_areas = area_counts.sort_values()
# Identify best 3 and worst 3 areas
# best_3 = sorted_areas[-4:].index
# worst_3 = sorted_areas[:4].index
# Plot areas and their percentages
for i in range(3):
for j in range(4):
area_index = i + j * 3
if area_index in area_percentages.index:
percentage = area_percentages[area_index]
count = area_counts[area_index]
color = cmap(norm(percentage))
else:
percentage = 0.0
count = 0
color = 'none'
x_left = bins_x[i]
x_right = bins_x[i + 1]
y_bottom = bins_y[j]
y_top = bins_y[j + 1]
x_center = (x_left + x_right) / 2
y_center = (y_bottom + y_top) / 2
# Draw the area rectangle with color
rect = plt.Rectangle((x_left, y_bottom), x_right - x_left, y_top - y_bottom,
linewidth=1, edgecolor='white', facecolor=color, linestyle='--', alpha=0.5)
ax.add_patch(rect)
# Annotate the percentage inside the rectangle
pitch.annotate(f'{percentage:.1f}%\n({count})', xy=(x_center, y_center), ax=ax, fontsize=12, ha='center',
va='center', color='black', zorder=3)
ax.annotate('Attack', xy=(50, 100), xytext=(50, 110),
arrowprops=dict(facecolor='red', shrink=0.05),
fontsize=12, ha='center')
plt.title(f'Percentage of Passes Received in Each Area\n{player_name}'
'→ Attack Direction', fontsize=16, ha='center', va='center')
ax.annotate('', xy=(120, 40), xytext=(0, 40),
arrowprops=dict(facecolor='red', shrink=0.05, width=2, headwidth=10, headlength=10),
fontsize=12, ha='center', va='center', zorder=4)
plt.tight_layout()
plt.show()
st.pyplot(plt.gcf())
# Example usage
# match_id = 3942382 # Replace with the match ID you are interested in
# player_name_local = 'Arda Güler' # Replace with the player name you are interested in
# #
# match_id_list =[]
# all_matches = sb.matches(competition_id=55,
# season_id=282)
# team_matches = all_matches[(all_matches['home_team'] == 'Turkey') | (all_matches['away_team'] == 'Turkey')]
# for match_id in team_matches['match_id']:
# match_id_list.append(match_id)
# get_passes(match_id_list, player_name_local)
# area_counts, bins_x, bins_y = calculate_area_percentages(passes, player_name)
# plot_area_percentages(area_counts, bins_x, bins_y)