-
Notifications
You must be signed in to change notification settings - Fork 1
/
2PlotShotsAndPasses.py
104 lines (82 loc) · 3.34 KB
/
2PlotShotsAndPasses.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
#Make a shot map and a pass map using Statsbomb data
#Set match id in match_id_required.
#Function to draw the pitch
import matplotlib.pyplot as plt
import numpy as np
#Size of the pitch in yards (!!!)
pitchLengthX=120
pitchWidthY=80
#ID for England vs Sweden Womens World Cup
match_id_required = 3749448
home_team_required ="Arsenal"
away_team_required ="Liverpool"
# Load in the data
# I took this from https://znstrider.github.io/2018-11-11-Getting-Started-with-StatsBomb-Data/
file_name=str(match_id_required)+'.json'
#Load in all match events
import json
with open('Statsbomb/data/events/'+file_name) as data_file:
#print (mypath+'events/'+file)
data = json.load(data_file)
#get the nested structure into a dataframe
#store the dataframe in a dictionary with the match id as key (remove '.json' from string)
from pandas import json_normalize
df = json_normalize(data, sep = "_").assign(match_id = file_name[:-5])
#A dataframe of shots
shots = df.loc[df['type_name'] == 'Shot'].set_index('id')
#Draw the pitch
from FCPython import createPitch
(fig,ax) = createPitch(pitchLengthX,pitchWidthY,'yards','gray')
#Plot the shots
for i,shot in shots.iterrows():
x=shot['location'][0]
y=shot['location'][1]
goal=shot['shot_outcome_name']=='Goal'
team_name=shot['team_name']
circleSize=2
circleSize=np.sqrt(shot['shot_statsbomb_xg'])*5
if (team_name==home_team_required):
if goal:
shotCircle=plt.Circle((x,pitchWidthY-y),circleSize,color="red")
shotCircle.set_alpha(.6)
plt.text((x+1),pitchWidthY-y+1,shot['player_name'])
else:
shotCircle=plt.Circle((x,pitchWidthY-y),circleSize,color="red")
shotCircle.set_alpha(.2)
elif (team_name==away_team_required):
if goal:
shotCircle=plt.Circle((pitchLengthX-x,y),circleSize,color="blue")
shotCircle.set_alpha(.6)
plt.text((pitchLengthX-x+1),y+1,shot['player_name'])
else:
shotCircle=plt.Circle((pitchLengthX-x,y),circleSize,color="blue")
shotCircle.set_alpha(.2)
ax.add_patch(shotCircle)
plt.text(5,75,away_team_required + ' shots')
plt.text(80,75,home_team_required + ' shots')
fig.set_size_inches(10, 7)
fig.savefig('Output/shots-Arsenal.pdf', dpi=100)
plt.show()
#Exercise:
#1, Create a dataframe of passes which contains all the passes in the match
passes = df.loc[df['type_name'] == 'Pass'].set_index('id')
#Draw the pitch
(fig,ax) = createPitch(pitchLengthX,pitchWidthY,'yards','gray')
for i,thepass in passes.iterrows():
#if thepass['team_name']==away_team_required: #
if thepass['player_name']=='Thierry Henry':
x=thepass['location'][0]
y=thepass['location'][1]
passCircle=plt.Circle((x,pitchWidthY-y),2,color="blue")
passCircle.set_alpha(.2)
ax.add_patch(passCircle)
dx=thepass['pass_end_location'][0]-x
dy=thepass['pass_end_location'][1]-y
passArrow=plt.Arrow(x,pitchWidthY-y,dx,-dy,width=3,color="blue")
ax.add_patch(passArrow)
fig.set_size_inches(10, 7)
fig.savefig('Output/passes-henry.pdf', dpi=100)
plt.show()
#2, Plot the start point of every Sweden pass. Attacking left to right.
#3, Plot only passes made by Caroline Seger (she is Sara Caroline Seger in the database)
#4, Plot arrows to show where the passes we