3. pandas -- module to work with dataframes. 4. FCPython -- module to create football pitch map """ import matplotlib.pyplot as plt import json from pandas.io.json import json_normalize from FCPython import createPitch ## Note Statsbomb data uses yards for their pitch dimensions pitch_length_X = 120 pitch_width_Y = 80 ## calling the function to create a pitch map ## yards is the unit for measurement and ## gray will be the line color of the pitch map (fig, ax) = createPitch(pitch_length_X, pitch_width_Y, 'yards', 'gray') ## match id for our El Clasico match_id = 69249 home_team = 'Real Madrid' away_team = 'Barcelona' player_name = 'Xavier Hernández Creus' ## this is the name of our event data file for ## our required El Clasico file_name = str(match_id) + '.json' ## loading the required event data file my_data = json.load( open('../Statsbomb/data/events/' + file_name, 'r', encoding='utf-8'))
pitchLengthX = 120 pitchWidthY = 80 OldRange = 1.0 - 0.0 NewRange_X = 120 - 0 NewRange_Y = 80 - 0 shots['X'] = shots['X'].apply(lambda x: (((x - 0.0) * NewRange_X) / OldRange) + 0.0) shots['Y'] = shots['Y'].apply(lambda x: (((x - 0.0) * NewRange_Y) / OldRange) + 0.0) #print(shots['result'].unique()) #Plotting two plots for goals scored and shots that were not goals (plt1, fig1, ax1) = createPitch(pitchLengthX, pitchWidthY, 'yards', 'black') (plt2, fig2, ax2) = createPitch(pitchLengthX, pitchWidthY, 'yards', 'black') for i, shot in shots.iterrows(): x = shot['X'] y = shot['Y'] #circleSize = 1.5 circleSize = np.sqrt(shot['xG']) * 2 if shot['result'] == 'ShotOnPost': shotCircle = plt.Circle((x, y), circleSize, color="cyan") ax1.add_patch(shotCircle) if shot['result'] == 'Goal': if shot['shotType'] == 'Head': shotCircle = plt.Circle((x, y), circleSize, color="green")
with open('/Users/lindelwemoyo/SoccermaticsForPython/Statsbomb/data/events/' + file_name) as data_file: #print (mypath+'events/'+file) data = json.load(data_file) #get nested structure into a dataframe #store the dataframe in a dictionary with the match id as key (remove .json from string) from pandas.io.json 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') #PlotTheShots 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),
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.io.json 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') #A dataframe of passes #passes = df.loc[df['type_name'] == 'Pass'].set_index('id') #Draw the pitch from FCPython import createPitch (fig, ax) = createPitch(pitchLengthX, pitchWidthY, 'yards', 'black') #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'] #draw arrows on the pass to show direction destinationx = shot['shot_end_location'][0] - x destinationy = shot['shot_end_location'][1] - y circleSize = 1