# Setup the pitch and number of bins pitch = Pitch(pitch_type='statsbomb', line_zorder=2, line_color='#c7d5cc', pitch_color='#22312b') bins = (6, 4) ############################################################################## # Plotting using a single color and length fig, ax = pitch.draw(figsize=(16, 11), constrained_layout=True, tight_layout=False) fig.set_facecolor('#22312b') # 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) ##############################################################################
# Calculate the percentages for each team and sort so that the teams which press higher are last df[pressure_cols] = df[pressure_cols].divide(df[pressure_cols].sum(axis=1), axis=0) * 100. df.sort_values(['Att 3rd', 'Def 3rd'], ascending=[True, False], inplace=True) ############################################################################## # Plot the percentages # setup a mplsoccer pitch pitch = Pitch(line_zorder=2, line_color='black') # 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)) # Plot fig, axes = pitch.draw(figsize=(16, 9), ncols=5, nrows=4, tight_layout=False, constrained_layout=True) 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[:len(teams)]): ax.set_title(teams[i], fontsize=20) # fill in the bin statistics from df and plot the heatmap
_ = axes[2].axis('off') _ = axes[3].axis('off') ############################################################################## # Heatmap shot map with histogram/ kdeplot on the marginal axes # ------------------------------------------------------------- fig, axes, _, _ = pitch.jointgrid( figheight=10, left=None, bottom=0.075, grid_height=0.8, # plot without endnote/ title axes title_height=0, endnote_height=0) bs1 = pitch.bin_statistic(df_team1.x, df_team1.y, bins=(18, 12)) bs2 = pitch.bin_statistic(df_team2.x, df_team2.y, bins=(18, 12)) # get the min/ max values for normalizing across both teams vmax = max(bs2['statistic'].max(), bs1['statistic'].max()) vmin = max(bs2['statistic'].min(), bs1['statistic'].min()) # set values where zero shots to nan values so it does not show up in the heatmap # i.e. zero values take the background color bs1['statistic'][bs1['statistic'] == 0] = np.nan bs2['statistic'][bs2['statistic'] == 0] = np.nan # set the vmin/ vmax so the colors depend on the minimum/maximum value for both teams hm1 = pitch.heatmap(bs1, ax=axes[0], cmap='Reds', vmin=vmin, vmax=vmax, edgecolor='#f9f9f9')
'related_event_df': False, 'shot_freeze_frame_df': False, 'tactics_lineup_df': False, 'warn': False } df = pd.concat([ read_event(f'{EVENT_SLUG}/{file}', **kwargs)['event'] for file in match_files ]) # filter chelsea pressure events mask_chelsea_pressure = (df.team_name == 'Chelsea FCW') & (df.type_name == 'Pressure') df = df.loc[mask_chelsea_pressure, ['x', 'y']] ############################################################################## # Plot the heatmaps # setup pitch pitch = Pitch(pitch_type='statsbomb', line_zorder=2, line_color='white') # draw fig, ax = pitch.draw(figsize=(16, 9)) bin_statistic = pitch.bin_statistic(df.x, df.y, statistic='count', bins=(25, 25)) bin_statistic['statistic'] = gaussian_filter(bin_statistic['statistic'], 1) pcm = pitch.heatmap(bin_statistic, ax=ax, cmap='hot', edgecolors='#22312b') cbar = fig.colorbar(pcm, ax=ax) TITLE_STR = 'Location of pressure events - 3 home games for Chelsea FC Women' title = fig.suptitle(TITLE_STR, x=0.4, y=0.98, fontsize=23)