43 KiB
43 KiB
pip install pandas
Requirement already satisfied: pandas in c:\users\piotr\anaconda3\lib\site-packages (1.3.5) Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\piotr\anaconda3\lib\site-packages (from pandas) (2.8.2) Requirement already satisfied: pytz>=2017.3 in c:\users\piotr\anaconda3\lib\site-packages (from pandas) (2021.3) Requirement already satisfied: numpy>=1.17.3 in c:\users\piotr\appdata\roaming\python\python38\site-packages (from pandas) (1.21.4) Requirement already satisfied: six>=1.5 in c:\users\piotr\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.16.0) Note: you may need to restart the kernel to use updated packages.
import pandas as pd
import json
with open('../open-data/data/events/3749068.json') as f:
match = json.load(f)
df = pd.json_normalize(match, sep='_').assign(match_id="7567")
shots = df[df.type_name == 'Shot'].set_index('id')
import numpy as np
import matplotlib.pyplot as plt
from FCPython import createPitch
pitch_width = 120
pitch_height = 80
fig, ax = createPitch(pitch_width, pitch_height, 'yards', 'gray')
home_team = 'Tottenham Hotspur'
away_team = 'Arsenal'
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']
circle_size = np.sqrt(shot['shot_statsbomb_xg'] * 30)
if team_name == home_team:
if goal:
shot_circle = plt.Circle((x, pitch_height-y), circle_size, color='grey')
plt.text((x+1), pitch_height-y+1, shot['player_name'])
else:
shot_circle = plt.Circle((x, pitch_height-y), circle_size, color='grey')
shot_circle.set_alpha(.1)
elif team_name == away_team:
if goal:
shot_circle = plt.Circle((pitch_width-x, y), circle_size, color='red')
plt.text((pitch_width-x+1), y+1, shot['player_name'])
else:
shot_circle = plt.Circle((pitch_width-x, y), circle_size, color='red')
shot_circle.set_alpha(.1)
ax.add_patch(shot_circle)
plt.text(5, 75, away_team)
plt.text(100, 75, home_team)
plt.title('Arsenal vs Tottenham Hotspur shots with expected goals size')
fig.set_size_inches(15, 11)
plt.show()