-
Notifications
You must be signed in to change notification settings - Fork 12
/
utility_function_viz.py
238 lines (189 loc) · 8.8 KB
/
utility_function_viz.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
Created on Sat Apr 18 20:09:17 2020
@author: slothfulwave612
This Python module will contain function for visualization.
Module Used(2):
---------------
1. matplotlib -- plotting library in Python.
2. numpy -- numerical computing library.
"""
import numpy as np
import matplotlib.pyplot as plt
def plot_pitch():
'''
Function to plot a football pitch, here we have
used all the dimensions in meters by converting
from yards.
Returns:
fig, ax -- figure and axis object
'''
## creating a figure
fig, ax = plt.subplots(figsize=(12,8))
## defining some of the parameters, will be used while plotting
field_dims = (105, 68)
field_color = 'mediumseagreen'
linewidth = 2
## field dimensions in meters
border_dims = (3, 3) ## border around the field of 3x3 meters
meters_per_yard = 0.9144 ## unit conversion from yards to meters
half_pitch_length = field_dims[0] / 2 ## length of half pitch
half_pitch_width = field_dims[1] / 2 ## width of half pitch
signs = [-1, 1] ## to plot pitch taking origin as center point
## Football field dimensions are typically defined in yards, so converting them to meters
goal_line_width = 8 * meters_per_yard
box_width = 20 * meters_per_yard
box_length = 6 * meters_per_yard
area_width = 44 * meters_per_yard
area_length = 18 * meters_per_yard
penalty_spot = 12 * meters_per_yard
corner_radius = meters_per_yard
D_length = 8 * meters_per_yard
D_radius = 10 * meters_per_yard
D_pos = 12 * meters_per_yard
center_circle_radius = 10 * meters_per_yard
## setting the background color to 'mediumseagreen'
ax.set_facecolor(field_color)
## plot the half way line and the center circle
ax.plot([0, 0], [-half_pitch_width, half_pitch_width], 'whitesmoke', linewidth)
ax.scatter(0, 0, marker='o', facecolor='whitesmoke', s=20)
## x and y coordinates for center circle
y = np.linspace(-1, 1, 50) * center_circle_radius
x = np.sqrt(center_circle_radius**2 - y**2)
## plotting the center circle
ax.plot(x, y, 'whitesmoke', linewidth)
ax.plot(-x, y, 'whitesmoke', linewidth)
## plot each halves
for s in signs:
## plotting pitch boundary
ax.plot([-half_pitch_length, half_pitch_length], [s * half_pitch_width, s * half_pitch_width], 'whitesmoke', linewidth)
ax.plot([s * half_pitch_length, s * half_pitch_length], [- half_pitch_width, half_pitch_width], 'whitesmoke', linewidth)
## plot the goal post
ax.plot([s * half_pitch_length, s * half_pitch_length], [-goal_line_width / 2, goal_line_width / 2], 'ws', linewidth, markersize=6)
## plotting six yard box
ax.plot([s * half_pitch_length, s * half_pitch_length - s * box_length], [box_width / 2, box_width / 2], 'whitesmoke', linewidth)
ax.plot([s * half_pitch_length, s * half_pitch_length - s * box_length], [-box_width / 2, -box_width / 2], 'whitesmoke', linewidth)
ax.plot([s * half_pitch_length - s * box_length, s * half_pitch_length - s * box_length], [box_width / 2, -box_width / 2], 'whitesmoke', linewidth)
## plotting penalty area
ax.plot([s * half_pitch_length, s * half_pitch_length - s * area_length], [area_width / 2, area_width / 2], 'whitesmoke', linewidth)
ax.plot([s * half_pitch_length, s * half_pitch_length - s * area_length], [- area_width / 2, - area_width / 2], 'whitesmoke', linewidth)
ax.plot([s * half_pitch_length - s * area_length, s * half_pitch_length - s * area_length], [area_width / 2, - area_width / 2], 'whitesmoke', linewidth)
## plotting penalty spot
ax.scatter(s * half_pitch_length - s * penalty_spot, 0, marker='o', facecolor='whitesmoke', s=20)
## plotting corner flag
y = np.linspace(0,1,50) * corner_radius
x = np.sqrt(corner_radius**2 - y**2)
ax.plot(s * half_pitch_length - s * x, -half_pitch_width + y, 'whitesmoke', linewidth)
ax.plot(s * half_pitch_length - s * x, half_pitch_width - y, 'whitesmoke', linewidth)
## plotting the D
y = np.linspace(-1, 1, 50) * D_length
x = np.sqrt(D_radius**2 - y**2) + D_pos
ax.plot(s * half_pitch_length - s * x, y, 'whitesmoke', linewidth)
## removing the ticks and the axis labels
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
## setting the axis limits
x_max = (field_dims[0] / 2) + border_dims[0]
y_max = (field_dims[1] / 2) + border_dims[0]
ax.set_xlim([-x_max, x_max])
ax.set_ylim([-y_max, y_max])
ax.set_axisbelow(True)
return fig, ax
def shot_map(df, fig, ax):
'''
This function will plot the shot map for the given shot
dataframe.
Argument:
df -- dataframe object, the dataframe passed(home or away).
fig -- figure object.
ax -- axis object.
Returns:
fig, ax -- figure and axis object.
'''
fig, ax = fig, ax
## iterating thorugh the shot dataframe
for row_num, shot in df.iterrows():
if shot['Subtype'][-5:] == '-GOAL':
color = 'bo'
label = 'Goal'
else:
color = 'ro'
label = 'No Goal'
if shot['Period'] == 2:
dist = 1
else:
dist = -1
## plotting the point where the shot took place
plt.plot(shot['Start X'] * dist, shot['Start Y'] * dist, color, label=label)
## adding an arrow to see the direction of the shot
ax.annotate("", xy=shot[['End X', 'End Y']] * dist, xytext=shot[['Start X', 'Start Y']] * dist,
arrowprops=dict(arrowstyle='->', color=color[0]))
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), loc='best', bbox_to_anchor=(0.9, 1, 0, 0), fontsize=12)
return fig, ax
def plot_events(df, fig, ax):
'''
Function to plot events.
Arguments:
df -- dataframe object, the dataframe passed
fig -- figure object
ax -- axis object
Returns:
fig -- figure object
ax -- axis object
'''
count = 1
for row_num, event in df.iterrows():
## iterating through each row
if event['Period'] == 2:
dist = 1
else:
dist = -1
if event['Type'] == 'SHOT' and event['Subtype'][-5:] == '-GOAL':
color = 'bo'
else:
color = 'ro'
## plotting the events
plt.plot(event['Start X'] * dist, event['Start Y'] * dist, color)
## adding arrows
ax.annotate('{}'.format(count), xy=event[['End X', 'End Y']] * dist, xytext=event[['Start X', 'Start Y']] * dist,
arrowprops=dict(arrowstyle='->', color=color[0]))
count += 1
return fig, ax
def plot_frame(home_team_loc, away_team_loc, fig, ax, event_row=None):
'''
Function will plot a frame for all 11 players at the pitch at
any given time.
Arguments:
home_team_loc -- row for home team frame.
away_team_loc -- row for away team frame.
fig, ax -- figure and axis object.
event_row -- row when particular event has occurred.
Retrns:
fig, ax -- figure and axis object.
'''
team_colors = ('r', 'b')
## red for home team and blue for away team
if home_team_loc['Period'] == 2:
dist = 1
else:
dist = -1
for team, color in zip([home_team_loc, away_team_loc], team_colors):
x_cols = [col for col in team.keys() if col[-1] == 'X' and col != 'ball_X']
y_cols = [col for col in team.keys() if col[-1] == 'Y' and col != 'ball_Y']
if color == 'r':
label = 'Home Team'
else:
label = 'Away Team'
ax.plot(team[x_cols] * dist, team[y_cols] * dist, color+'o', MarkerSize=10, label=label)
ax.plot(home_team_loc['ball_X'] * dist, away_team_loc['ball_Y'] * dist, 'ko', MarkerSize=6)
if event_row is not None:
ax.annotate('', xy=event_row[['End X', 'End Y']] * dist,
xytext=event_row[['Start X', 'Start Y']] * dist,
arrowprops=dict(arrowstyle='->', color='m'))
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys(), loc='best', bbox_to_anchor=(0.9, 1, 0, 0), fontsize=12)
return fig, ax