.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/lesson6/plot_VoronoiDiagrams.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_lesson6_plot_VoronoiDiagrams.py: Making Voronoi Diagrams ===================== Making Voronoi Diagrams with Statsbomb 360 data .. GENERATED FROM PYTHON SOURCE LINES 7-13 .. code-block:: default from mplsoccer import Sbopen, VerticalPitch import numpy as np import matplotlib.pyplot as plt # The first thing we have to do is open the data. We use a parser SBopen available in mplsoccer. .. GENERATED FROM PYTHON SOURCE LINES 14-22 Opening data ---------------------------- For this task we will use Statsbomb 360 data form Sweden against Switzerland game at the Women's UEFA Euro 2022. We want to make the plot for Bennison's goal from that game. We also take the *id* of this event. As the next step we open the 360 data. In *df_frame* player location is stored and in *df_visible* area tracked by Statsbomb during this event. From the latter we take visible area only for this specific event and store it as a numpy array with apeces coordinates stored in separate rows. .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: default #declare mplsoccer parser parser = Sbopen() #open event dataset df_event = parser.event(3835331)[0] #find Bennison goal event = df_event.loc[df_event["outcome_name"] == 'Goal'].loc[df_event["player_name"] == 'Hanna Ulrika Bennison'] #save it's id event_id = event["id"].iloc[0] #open 360 df_frame, df_visible = parser.frame(3835331) #get visible area visible_area = np.array(df_visible.loc[df_visible["id"] == event_id]['visible_area'].iloc[0]).reshape(-1, 2) .. GENERATED FROM PYTHON SOURCE LINES 39-42 Plotting visible area ---------------------------- To investigate the area that Statsbomb managed to catch, we plot it using *polygon* method of mplsoccer. .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: default pitch = VerticalPitch(line_color='grey', line_zorder = 1, half = True, pad_bottom=-30, linewidth=5) fig, ax = pitch.grid(grid_height=0.9, title_height=0.06, axis=False, endnote_height=0.04, title_space=0, endnote_space=0) #add visible area pitch.polygon([visible_area], color=(0, 0, 1, 0.3), ax=ax["pitch"], zorder = 2) fig.suptitle("Area catched by Statsbomb 360 data - Hanna Bennison's goal", fontsize = 45) plt.show() .. image-sg:: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_001.png :alt: Area catched by Statsbomb 360 data - Hanna Bennison's goal :srcset: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-57 Plotting player position ---------------------------- Now, to get a better understanding of Statsbomb 360 data, we can plot player position during the shot as well as shot trajectory. .. GENERATED FROM PYTHON SOURCE LINES 57-79 .. code-block:: default #get player position for this event player_position = df_frame.loc[df_frame["id"] == event_id] #get swedish player position sweden = player_position.loc[player_position["teammate"] == True] #get swiss player positions swiss = player_position.loc[player_position["teammate"] == False] fig, ax = pitch.grid(grid_height=0.9, title_height=0.06, axis=False, endnote_height=0.04, title_space=0, endnote_space=0) #plot visible area pitch.polygon([visible_area], color=(0, 0, 1, 0.3), ax=ax["pitch"], zorder = 2) #plot sweden players - yellow pitch.scatter(sweden.x, sweden.y, color = 'yellow', edgecolors = 'black', s = 400, ax=ax['pitch'], zorder = 3) #plot swiss players - red pitch.scatter(swiss.x, swiss.y, color = 'red', edgecolors = 'black', s = 400, ax=ax['pitch'], zorder = 3) #add shot pitch.lines(event.x, event.y, event.end_x, event.end_y, comet = True, color='green', ax=ax['pitch'], zorder = 1, linestyle = ':', lw = 2) fig.suptitle("Player position during Benninson's goal", fontsize = 45) plt.show() .. image-sg:: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_002.png :alt: Player position during Benninson's goal :srcset: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-84 Plotting Voronoi diagrams for 1 team. ---------------------------- Now, we can make Voronoi diagrams for Swedish teams. We do it using *voronoi* method. Then, we clip the diagram to restricted area only. .. GENERATED FROM PYTHON SOURCE LINES 84-107 .. code-block:: default #Voronoi for Sweden team1, team2 = pitch.voronoi(sweden.x, sweden.y, sweden.teammate) fig, ax = pitch.grid(grid_height=0.9, title_height=0.06, axis=False, endnote_height=0.04, title_space=0, endnote_space=0) #plot voronoi diagrams as polygons t1 = pitch.polygon(team1, ax = ax["pitch"], color = 'yellow', ec = 'black', lw=3, alpha=0.4, zorder = 2) #mark visible area visible = pitch.polygon([visible_area], color = 'None', linestyle = "--", ec = "black", ax=ax["pitch"], zorder = 2) #plot swedish players pitch.scatter(sweden.x, sweden.y, color = 'yellow', edgecolors = 'black', s = 600, ax=ax['pitch'], zorder = 4) #plot swiss players pitch.scatter(swiss.x, swiss.y, color = 'red', edgecolors = 'black', s = 600, ax=ax['pitch'], zorder = 3) #plot shot pitch.lines(event.x, event.y, event.end_x, event.end_y, comet = True, color='green', ax=ax['pitch'], zorder = 1, linestyle = ':', lw = 5) #limit voronoi diagram to polygon for p1 in t1: p1.set_clip_path(visible[0]) fig.suptitle("Voronoi diagram for Sweden (in the visible area) - Hanna Bennison's goal", fontsize = 30) plt.show() .. image-sg:: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_003.png :alt: Voronoi diagram for Sweden (in the visible area) - Hanna Bennison's goal :srcset: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 108-112 Plotting Voronoi diagrams for 2 teams. ---------------------------- We can also differentiate between areas and mark areas that each player was the closest to. To do that instead of using a dataframe with only one team players' position, we use both teams. .. GENERATED FROM PYTHON SOURCE LINES 112-139 .. code-block:: default #voronoi for both teams team1, team2 = pitch.voronoi(player_position.x, player_position.y, player_position.teammate) fig, ax = pitch.grid(grid_height=0.9, title_height=0.06, axis=False, endnote_height=0.04, title_space=0, endnote_space=0) #add sweden t1 = pitch.polygon(team1, ax = ax["pitch"], color = 'yellow', ec = 'black', lw=3, alpha=0.4, zorder = 2) #add switzerland t2 = pitch.polygon(team2, ax = ax["pitch"], color = 'red', ec = 'black', lw=3, alpha=0.4, zorder = 2) #mark visible area visible = pitch.polygon([visible_area], color = 'None', linestyle = "--", ec = "black", ax=ax["pitch"], zorder = 2) #plot swedish players pitch.scatter(sweden.x, sweden.y, color = 'yellow', edgecolors = 'black', s = 600, ax=ax['pitch'], zorder = 4) #plot swiss players pitch.scatter(swiss.x, swiss.y, color = 'red', edgecolors = 'black', s = 600, ax=ax['pitch'], zorder = 3) #plot shot pitch.lines(event.x, event.y, event.end_x, event.end_y, comet = True, color='green', ax=ax['pitch'], zorder = 1, linestyle = ':', lw = 5) #clip sweden for p1 in t1: p1.set_clip_path(visible[0]) #clip sswitzerland for p2 in t2: p2.set_clip_path(visible[0]) fig.suptitle("Voronoi diagram for both teams (in the visible area) - Hanna Bennison's goal", fontsize = 30) plt.show() .. image-sg:: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_004.png :alt: Voronoi diagram for both teams (in the visible area) - Hanna Bennison's goal :srcset: /gallery/lesson6/images/sphx_glr_plot_VoronoiDiagrams_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.817 seconds) .. _sphx_glr_download_gallery_lesson6_plot_VoronoiDiagrams.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_VoronoiDiagrams.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_VoronoiDiagrams.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_