.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch_plots/plot_heatmap_positional.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_pitch_plots_plot_heatmap_positional.py: ========================= Heatmap Juego de Posición ========================= This example shows how to plot all pressure events from three matches as a `Juego de Posición `_ heatmap. .. GENERATED FROM PYTHON SOURCE LINES 9-25 .. code-block:: Python import matplotlib.patheffects as path_effects import matplotlib.pyplot as plt import pandas as pd from matplotlib.colors import LinearSegmentedColormap from mplsoccer import VerticalPitch, FontManager, Sbopen # get data parser = Sbopen() match_files = [19789, 19794, 19805] df = pd.concat([parser.event(file)[0] for file in match_files]) # 0 index is the event file # filter chelsea pressure events mask_chelsea_pressure = (df.team_name == 'Chelsea FCW') & (df.type_name == 'Pressure') df = df.loc[mask_chelsea_pressure, ['x', 'y']] .. GENERATED FROM PYTHON SOURCE LINES 26-27 Custom colormap, font, and path effects .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python # see the custom colormaps example for more ideas on setting colormaps pearl_earring_cmap = LinearSegmentedColormap.from_list("Pearl Earring - 10 colors", ['#15242e', '#4393c4'], N=10) # fontmanager for google font (robotto) robotto_regular = FontManager() path_eff = [path_effects.Stroke(linewidth=3, foreground='black'), path_effects.Normal()] .. GENERATED FROM PYTHON SOURCE LINES 39-41 Plot positional heatmap ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-55 .. code-block:: Python # setup pitch pitch = VerticalPitch(pitch_type='statsbomb', line_zorder=2, pitch_color='#22312b', line_color='white') # draw fig, ax = pitch.draw(figsize=(4.125, 6)) bin_statistic = pitch.bin_statistic_positional(df.x, df.y, statistic='count', positional='full', normalize=True) pitch.heatmap_positional(bin_statistic, ax=ax, cmap='coolwarm', edgecolors='#22312b') pitch.scatter(df.x, df.y, c='white', s=2, ax=ax) labels = pitch.label_heatmap(bin_statistic, color='#f4edf0', fontsize=18, ax=ax, ha='center', va='center', str_format='{:.0%}', path_effects=path_eff) .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_heatmap_positional_001.png :alt: plot heatmap positional :srcset: /gallery/pitch_plots/images/sphx_glr_plot_heatmap_positional_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-59 Plot the chart again with a title --------------------------------- We will use mplsoccer's grid function to plot a pitch with a title and endnote axes. .. GENERATED FROM PYTHON SOURCE LINES 59-86 .. code-block:: Python pitch = VerticalPitch(pitch_type='statsbomb', line_zorder=2, pitch_color='#1e4259') fig, axs = pitch.grid(endnote_height=0.03, endnote_space=0, title_height=0.08, title_space=0, # Turn off the endnote/title axis. I usually do this after # I am happy with the chart layout and text placement axis=False, grid_height=0.84) fig.set_facecolor('#1e4259') # heatmap and labels bin_statistic = pitch.bin_statistic_positional(df.x, df.y, statistic='count', positional='full', normalize=True) pitch.heatmap_positional(bin_statistic, ax=axs['pitch'], cmap=pearl_earring_cmap, edgecolors='#22312b') labels = pitch.label_heatmap(bin_statistic, color='#f4edf0', fontsize=18, ax=axs['pitch'], ha='center', va='center', str_format='{:.0%}', path_effects=path_eff) # endnote and title axs['endnote'].text(1, 0.5, '@your_twitter_handle', va='center', ha='right', fontsize=15, fontproperties=robotto_regular.prop, color='#dee6ea') axs['title'].text(0.5, 0.5, "Pressure applied by\n Chelsea FC Women", color='#dee6ea', va='center', ha='center', path_effects=path_eff, fontproperties=robotto_regular.prop, fontsize=25) # sphinx_gallery_thumbnail_path = 'gallery/pitch_plots/images/sphx_glr_plot_heatmap_positional_002.png' plt.show() # If you are using a Jupyter notebook you do not need this line .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_heatmap_positional_002.png :alt: plot heatmap positional :srcset: /gallery/pitch_plots/images/sphx_glr_plot_heatmap_positional_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.839 seconds) .. _sphx_glr_download_gallery_pitch_plots_plot_heatmap_positional.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_heatmap_positional.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_heatmap_positional.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_heatmap_positional.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_