In [1]:
#import necessary packages
import requests
from sports_plotter.soccer import Pitch
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
#get statsbomb womens world cup final play-by-play and convert to json file
url = 'https://raw.githubusercontent.com/statsbomb/open-data/master/data/events/69321.json'
game = requests.get(url).json()
In [3]:
#take data until substitution is made so you only have 11 players in the pass network
game = game[:game.index([x for x in game if x['type']['name'] == 'Substitution'][0])]
In [4]:
#get all completed passes
passes = [x for x in game if x['type']['name'] == 'Pass']
passes = [x for x in passes if 'recipient' in x['pass'].keys()]
In [5]:
#seperate passes by team
usa = [x for x in passes if x['team']['name'] == "United States Women's"]
neth = [x for x in passes if x['team']['name'] == "Netherlands Women's"]
In [6]:
#get locations of usa passes
usa_locs = [(x['location'] + x['pass']['end_location']) for x in usa]
#get passer ids for usa passes
usa_pass_ids = [(x['player']['id']) for x in usa]
#get reciever ids for usa passes
usa_rec_ids = [(x['pass']['recipient']['id']) for x in usa]
#get locations of netherlands passes
neth_locs = [(x['location'] + x['pass']['end_location']) for x in neth]
#get passer ids for netherlands passes
neth_pass_ids = [(x['player']['id']) for x in neth]
#get reciever ids for netherlands passes
neth_rec_ids = [(x['pass']['recipient']['id']) for x in neth]
In [8]:
#create a 1x2 grid of plots
fig, ax = plt.subplots(1,2)
#set width of grid to be 15 inches, not really 15 inches
fig.set_figwidth(15)


#create pitch object for usa pass network
pc_usa = Pitch()
#plot usa pass network
pc_usa.pass_network(usa_locs, usa_pass_ids, usa_rec_ids)


#create pitch object for netherlands pass network
pc_neth = Pitch()
#plot netherlands pass network
pc_neth.pass_network(neth_locs, neth_pass_ids, neth_rec_ids)


#plot usa pitch onto first subplot and netherlands pitch onto second subplot
pc_usa.show_pitch(ax[0])
pc_neth.show_pitch(ax[1])

#title plots
fig.suptitle("2019 FIFA Womens World Cup Final Passing Networks", fontsize=20)
ax[0].set_title("USA",y=-0.2)
ax[1].set_title("Netherlands", y=-0.2)

#show plots
plt.show()