def defensesmap(Df,team,teamid): df = Df.copy() pitch = Pitch(pitch_type='statsbomb', figsize=(10,6), line_zorder=2, layout=(1,2), line_color='k', orientation='horizontal',constrained_layout=True, tight_layout=False) fig, ax = pitch.draw() defensedf = df.query("(type_displayName in ['Aerial','Clearance','Interception','Tackle','BlockedPass','Challenge'])\ &(teamId==@teamid)").reset_index() defensedf['defensive'] = [1 for i in range(len(defensedf))] for i in range(len(defensedf)): if(defensedf.loc[i,['type_displayName']][0]=='Aerial'): quals = defensedf.quals[i] if(286 in quals): defensedf.loc[i,['defensive']] = 0 if(defensedf.loc[i,['type_displayName']][0]=='Challenge'): quals = defensedf.quals[i] if(286 in quals): defensedf.loc[i,['defensive']] = 0 defensedf = defensedf[defensedf.defensive==1] defensedf['y'] = 80 - defensedf['y'] ppda = defensedf[defensedf.x>=48] ppda = ppda.query("type_displayName in ['Interception','Tackle','Challenge']") deepdf = defensedf[defensedf.x<48] bin_statistic = pitch.bin_statistic(ppda.x, ppda.y, statistic='count', bins=(25, 25)) bin_statistic['statistic'] = gaussian_filter(bin_statistic['statistic'], 1) pcm = pitch.heatmap(bin_statistic, ax=ax[0], cmap='Reds', edgecolors=None) ax[0].set_title(team+'\n'+' Pressurizing'+'\n'+ 'Defensive activities',fontsize=30) bin_statistic = pitch.bin_statistic(deepdf.x, deepdf.y, statistic='count', bins=(25, 25)) bin_statistic['statistic'] = gaussian_filter(bin_statistic['statistic'], 1) pcm = pitch.heatmap(bin_statistic, ax=ax[1], cmap='Reds', edgecolors=None) # cbar = fig.colorbar(pcm, ax=ax[1]) ax[1].set_title(team+'\n'+' Goal Protecting'+'\n'+'Defensive activities',fontsize=30) fig.text(0.0, 0.0, "Created by Soumyajit Bose / @Soumyaj15209314",fontstyle="italic",fontsize=15,color='#edece9') fig.text(0.0, 0.15, "Pressurizing defensive activities include tackles, interceptions and challenges"+'\n'+ "made high up in opposition territory",fontstyle="italic",fontsize=15,color='#edece9') fig.text(0.0, 0.05, "Goal protecting defensive activities include tackles, interceptions, defensive aerials, challenges," +'\n'+"clearances and blocked passes deep in own territory",fontstyle="italic",fontsize=15,color='#edece9') # add_image(bu_alt, fig, left=0.9, bottom=0.01, width=0.07) return fig
def touchmap(Df,team,teamid): df = Df.copy() pitch = Pitch(pitch_type='statsbomb', figsize=(12,7), line_zorder=2, layout=(4,4), line_color='k', orientation='horizontal',constrained_layout=True, tight_layout=False) fig, ax = pitch.draw() touchdf = df[(df.isTouch==True)&(df.teamId==teamid)&(df.name!='')].reset_index() touchdf['y'] = 80 - touchdf['y'] players = touchdf.name.unique().tolist() Nplayers = len(players) for i in range(Nplayers): pdf = touchdf[touchdf.name==players[i]] bin_statistic = pitch.bin_statistic(pdf.x, pdf.y, statistic='count', bins=(25, 25)) bin_statistic['statistic'] = gaussian_filter(bin_statistic['statistic'], 1) pcm = pitch.heatmap(bin_statistic, ax=ax[i//4,i%4], cmap='Reds', edgecolors=None) ax[i//4,i%4].set_title(players[i],fontsize=15) for i in range(Nplayers,16): ax[i//4,i%4].remove() fig.text(0.0, 0.0, "Created by Soumyajit Bose / @Soumyaj15209314",fontstyle="italic",fontsize=15,color='#edece9') fig.text(0.3, 1.01, team+" Touch-based heatmaps",fontsize=25,color='#edece9') # add_image(bu_alt, fig, left=0.95, bottom=0.97, width=0.07) return fig
############################################################################## # Plot the percentages # setup a mplsoccer pitch pitch = Pitch(line_zorder=2, line_color='black', figsize=(16, 9), layout=(4, 5), tight_layout=False, constrained_layout=True) # mplsoccer calculates the binned statistics usually from raw locations, such as pressure events # for this example we will create a binned statistic dividing the pitch into thirds for one point (0, 0) # we will fill this in a loop later with each team's statistics from the dataframe bin_statistic = pitch.bin_statistic([0], [0], statistic='count', bins=(3, 1)) # load the StatsBomb logo sb_logo = Image.open( urlopen(( 'https://github.com/statsbomb/open-data/blob/fb54bd7fe20dbd5299fafc64f1f6f0d919a5e40d/' 'stats-bomb-logo.png?raw=true'))) # Plot fig, axes = pitch.draw() axes = axes.ravel() teams = df['Squad'].values vmin = df[pressure_cols].min().min( ) # we normalise the heatmaps with the min / max values vmax = df[pressure_cols].max().max() for i, ax in enumerate(axes):
# setup pitch pitch = Pitch(pitch_type='statsbomb', figsize=(16, 9), layout=(1, 3), line_zorder=2, pitch_color='#22312b', line_color='white', orientation='vertical') # draw fig, ax = pitch.draw() # heatmap specified by (nx, ny) for horizontal pitch bins = [(6, 5), (1, 5), (6, 1)] for i, bin in enumerate(bins): bin_statistic = pitch.bin_statistic(df.x, df.y, statistic='count', bins=bin) # draw pitch.heatmap(bin_statistic, ax=ax[i], cmap='coolwarm', edgecolors='#22312b') pitch.scatter(df.x, df.y, c='white', s=2, ax=ax[i]) # replace raw counts with percentages and add percentage sign (note immutable named tuple so used _replace) bin_statistic['statistic'] = (pd.DataFrame( (bin_statistic['statistic'] / bin_statistic['statistic'].sum() )).applymap(lambda x: '{:.0%}'.format(x)).values) pitch.label_heatmap(bin_statistic, color='white', fontsize=18, ax=ax[i],
pitch = Pitch(pitch_type='statsbomb', orientation='horizontal', figsize=(16, 11), line_zorder=2, line_color='#c7d5cc', constrained_layout=True, tight_layout=False, pitch_color='#22312b') bins = (6, 4) ############################################################################## # Plotting using a single color and length fig, ax = pitch.draw() # plot the heatmap - darker colors = more passes originating from that square bs_heatmap = pitch.bin_statistic(df_pass.x, df_pass.y, statistic='count', bins=bins) hm = pitch.heatmap(bs_heatmap, ax=ax, cmap='Blues') # plot the pass flow map with a single color ('black') and length of the arrow (5) fm = pitch.flow(df_pass.x, df_pass.y, df_pass.end_x, df_pass.end_y, color='black', arrow_type='same', arrow_length=5, bins=bins, ax=ax) ax.set_title(f'{team1} pass flow map vs {team2}', fontsize=30, pad=-20) fig.set_facecolor('#22312b')
xT_Players = barcamoves.groupby('name')[[ 'xT_value', 'end_zone_value', 'start_zone_value' ]].sum().sort_values('xT_value', ascending=False).round(4) pitch = Pitch(pitch_type='uefa', figsize=(6.8, 10.5), line_zorder=2, line_color='white', orientation='vertical') # draw fig, ax = pitch.draw() from matplotlib.colors import ListedColormap, LinearSegmentedColormap cmaplist = ['#082630', '#0682fe', "#eff3ff"] cmap = LinearSegmentedColormap.from_list("", cmaplist) bin_statistic = pitch.bin_statistic(barcamoves.x, barcamoves.y, values=barcamoves.xT_value, statistic='sum', bins=(38, 25)) bin_statistic['statistic'] = gaussian_filter(bin_statistic['statistic'], 1) vm = bin_statistic['statistic'].min() vma = bin_statistic['statistic'].max() pitch.heatmap(bin_statistic, ax=ax, cmap='inferno', edgecolors=None, vmin=bin_statistic['statistic'].min(), vmax=bin_statistic['statistic'].max()) ax.set_title('Barcelona' + '\n' + 'Open-play Threat-generation hotspots', fontsize=25) fig.set_facecolor('white') plt.savefig('xt.png', dpi=600)