-
Notifications
You must be signed in to change notification settings - Fork 3
/
shots.py
56 lines (42 loc) · 1.62 KB
/
shots.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
import matplotlib.pyplot as plt
import numpy as np
pitchLengthX=120
pitchWidthY=80
match_id_required = 7554
home_team_required ="England"
away_team_required ="Panama"
file_name=str(match_id_required)+'.json'
import json
with open('events/'+file_name) as data_file:
data = json.load(data_file)
from pandas.io.json import json_normalize
df = json_normalize(data, sep = "_").assign(match_id = file_name[:-5])
shots = df.loc[df['type_name'] == 'Shot'].set_index('id')
from FCPython import createPitch
(fig,ax) = createPitch(pitchLengthX,pitchWidthY,'yards','black')
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']*15)
if (team_name==home_team_required):
if goal:
shotCircle=plt.Circle((x,pitchWidthY-y),circleSize,color="red")
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")
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)
plt.show()