#!/usr/bin/env python # coding: utf-8 # In[61]: import pandas as pd from mplsoccer.pitch import Pitch # In[62]: #read in csv df = pd.read_csv('valladolidA.csv') # In[63]: #filter df to get only the team we want df = df[df['teamId']=='Barcelona'] # In[64]: #now we want to find our passes and recipients and then filter for only passes df['passer'] = df['playerId'] df['recipient'] = df['playerId'].shift(-1) #find passes and then only look for the successful passes passes = df[df['type']=='Pass'] successful = passes[passes['outcome']=='Successful'] # In[65]: successful # In[66]: #find the first subsititution and filter the successful dataframe to be less than that minute subs = df[df['type']=='SubstitutionOff'] subs = subs['minute'] firstSub = subs.min() successful = successful[successful['minute'] < firstSub] # In[67]: successful # In[68]: #this makes it so our passer and recipients are float values pas = pd.to_numeric(successful['passer'],downcast='integer') rec = pd.to_numeric(successful['recipient'],downcast='integer') successful['passer'] = pas successful['recipient'] = rec # In[69]: #now we need to find the average locations and counts of the passes average_locations = successful.groupby('passer').agg({'x':['mean'],'y':['mean','count']}) average_locations.columns = ['x','y','count'] # In[70]: average_locations # In[71]: successful # In[72]: #now we need to find the number of passes between each player pass_between = successful.groupby(['passer','recipient']).id.count().reset_index() pass_between.rename({'id':'pass_count'},axis='columns',inplace=True) #merge the average location dataframe. We need to merge on the passer first then the recipient pass_between = pass_between.merge(average_locations, left_on='passer',right_index=True) pass_between = pass_between.merge(average_locations, left_on='recipient',right_index=True,suffixes=['', '_end']) # In[73]: pass_between # In[74]: #set minimum threshold of combinations.. I like 5 for high passing teams. 2 or 3 for low passing. pass_between = pass_between[pass_between['pass_count']>5] # In[75]: pass_between # In[76]: #plot the pitch pitch = pitch = Pitch(pitch_type='statsbomb', orientation='horizontal', pitch_color='#22312b', line_color='#c7d5cc', figsize=(13, 8), constrained_layout=True, tight_layout=False) fig, ax = pitch.draw() #plot the arrows arrows = pitch.arrows(1.2*pass_between.x,.8*pass_between.y,1.2*pass_between.x_end,.8*pass_between.y_end, width = 5, headwidth = 5, color = 'w', ax = ax, zorder = 1, alpha = .5) #plot the nodes nodes = pitch.scatter(1.2*average_locations.x,.8*average_locations.y, s = 300, color = '#d3d3d3', edgecolors = 'black', linewidth = 2.5, alpha = 1, zorder = 1, ax=ax) #make annotations # In[ ]: