-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch_visualisation.py
179 lines (145 loc) · 5.76 KB
/
match_visualisation.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Data Handling & Analysis
import datetime
import numpy as np
import pandas as pd
# Data Visualisation
import matplotlib.pyplot as plt
import matplotlib.image as image
from matplotlib.patches import Arc
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
# Web Scraping
import json
import requests
from bs4 import BeautifulSoup
# Custom functions for extracting match info
import match_scraper
# Custom function for drawing a football pitch
from draw_football_pitch import draw_pitch
def create_figure(match_id, fig, ax):
"""Add the figure to the given axes object."""
soup_object, data_dict = match_scraper.scrape_script_data(match_id)
df_summary = match_scraper.extract_summary_stats(soup_object)
date = match_scraper.extract_date(data_dict)
headline = match_scraper.extract_headline(df_summary)
home_team, away_team = match_scraper.extract_team_names(df_summary)
crest_home, crest_away = match_scraper.get_crest_img(df_summary)
df_home = pd.DataFrame(data_dict['h'])
df_away = pd.DataFrame(data_dict['a'])
# Convert numeric columns to floats
float_cols = ['X', 'Y', 'xG']
for df in [df_home, df_away]:
df[float_cols] = df[float_cols].astype('float64')
# Isolate goals and shot data for both teams
goals_home = df_home[df_home['result'] == 'Goal']
shots_home = df_home[df_home['result'] != 'Goal']
goals_away = df_away[df_away['result'] == 'Goal']
shots_away = df_away[df_away['result'] != 'Goal']
bg_color = '#0f253a'
goal_color = 'red'
edgecolor = 'white'
plt.rcParams['text.color'] = 'white'
plt.rcParams['font.family'] = 'Century Gothic'
plt.rcParams.update({'font.size': 24})
fig.patch.set_facecolor(bg_color)
draw_pitch(pitch_color=bg_color, line_color='lightgrey', ax=ax)
### 01 - Shots and Goals ###
for i, df in enumerate([shots_home, goals_home]):
ax.scatter(x=105 - df['X'] * 105,
y=68 - df['Y'] * 68,
s=df['xG'] * 1024,
lw=[2, 1][i],
alpha=0.7,
facecolor=['none', goal_color][i],
edgecolor=edgecolor)
for i, df in enumerate([shots_away, goals_away]):
ax.scatter(x=df['X'] * 105,
y=df['Y'] * 68,
s=df['xG'] * 1024,
lw=[2, 1][i],
alpha=0.7,
facecolor=['none', goal_color][i],
edgecolor=edgecolor)
### 02 - Title & Subtitle ###
ax.text(x=0, y=75, s=headline, size=35, weight='bold')
ax.text(x=0, y=71, s='Premier League 2021-22 | {}'.format(date), size=20)
### 03 - Team Names ###
for i, team in zip([-1, 1], [home_team, away_team]):
ax.text(x=105 / 2 + i * 14,
y=63,
s=team,
size=35,
ha='center',
weight='bold')
### 04 - Team Logos ###
for i, img in zip([-1, 1], [crest_home, crest_away]):
imagebox = OffsetImage(img, zoom=0.4)
ab = AnnotationBbox(imagebox, (105 / 2 + i * 14, 56), frameon=False)
ax.add_artist(ab)
### 05 - Stats ###
features = ['Goals', 'xG', 'Shots', 'On Target', 'DEEP', 'xPTS']
for i, feature in enumerate(features):
if float(df_summary.loc[feature, 'Home']) > float(df_summary.loc[feature, 'Away']):
weights = ['bold', 'normal']
elif float(df_summary.loc[feature, 'Home']) < float(df_summary.loc[feature, 'Away']):
weights = ['normal', 'bold']
else:
weights = ['normal', 'normal']
ax.text(x=105 / 2,
y=46 - i * 8,
s=feature,
size=22,
ha='center',
va='center',
bbox=dict(facecolor='darkgray',
edgecolor=edgecolor,
alpha=0.85,
pad=0.6,
boxstyle='round'))
ax.text(x=105 / 2 - 14,
y=46 - i * 8,
s=df_summary.loc[feature, 'Home'],
size=20,
ha='center',
va='center',
weight=weights[0],
bbox=dict(facecolor='firebrick',
edgecolor='w',
alpha=0.6,
pad=0.6,
boxstyle='round'))
ax.text(x=105 / 2 + 14,
y=46 - i * 8,
s=df_summary.loc[feature, 'Away'],
size=20,
ha='center',
va='center',
weight=weights[1],
bbox=dict(facecolor='firebrick',
edgecolor='w',
alpha=0.6,
pad=0.6,
boxstyle='round'))
### 06 - Legend - Outcome ###
ax.text(x=105 / 4 + 0, y=-5, s='Outcome:', ha='center')
ax.text(x=105 / 4 - 8, y=-10, s='Shot', ha='center')
ax.text(x=105 / 4 + 8, y=-10, s='Goal', ha='center')
for i in range(2):
ax.scatter(x=[105 / 4 - 14, 105 / 4 + 1.5][i],
y=-8.8,
s=500,
lw=[2, 1][i],
alpha=0.7,
facecolor=[bg_color, goal_color][i],
edgecolor=edgecolor)
### 07 - Legend - xG value ###
ax.text(x=3 * 105 / 4, y=-5, s='xG Value:', ha='center')
for i in range(0, 5):
ax.scatter(x=[69.8, 73.4, 77.7, 82.4, 87.5][i],
y=-8.5,
s=((i + 1) * 0.2) * 500,
lw=2,
color=bg_color,
edgecolor=edgecolor)
### 08 - Legend - Credit ###
credit_text = 'Data: Understat | Konstantinos Orfanakis'
ax.text(x=105, y=-14, s=credit_text, size=16, ha='right')