.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch_plots/plot_voronoi.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_voronoi.py: ======================= Plots a Voronoi diagram ======================= This example shows how to plot a Voronoi diagram for a freeze frame. .. GENERATED FROM PYTHON SOURCE LINES 8-16 .. code-block:: Python import matplotlib.pyplot as plt from mplsoccer import Pitch, Sbopen import numpy as np # get freeze frame data for game 3788741 parser = Sbopen() frames, visible = parser.frame(3788741) .. GENERATED FROM PYTHON SOURCE LINES 17-18 Subset a shot .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python frame_idx = 50 frame_id = visible.iloc[50].id visible_area = np.array(visible.iloc[frame_idx].visible_area).reshape(-1, 2) player_position_data = frames[frames.id == frame_id] teammate_locs = player_position_data[player_position_data.teammate] opponent_locs = player_position_data[~player_position_data.teammate] .. GENERATED FROM PYTHON SOURCE LINES 29-30 Plotting .. GENERATED FROM PYTHON SOURCE LINES 30-55 .. code-block:: Python # draw plot p = Pitch(pitch_type='statsbomb') fig, ax = p.draw(figsize=(12,8)) # Plot Voronoi team1, team2 = p.voronoi(player_position_data.x, player_position_data.y, player_position_data.teammate) t1 = p.polygon(team1, ax=ax, fc='orange', ec='white', lw=3, alpha=0.4) t2 = p.polygon(team2, ax=ax, fc='dodgerblue', ec='white', lw=3, alpha=0.4) # Plot players sc1 = p.scatter(teammate_locs.x, teammate_locs.y, c='orange', s=80, ec='k', ax=ax) sc2 = p.scatter(opponent_locs.x, opponent_locs.y, c='dodgerblue', s=80, ec='k', ax=ax) # Plot the visible area visible = p.polygon([visible_area], color='None', ec='k', linestyle='--', lw=2, ax=ax) # clip each player to the visible area for p1 in t1: p1.set_clip_path(visible[0]) for p2 in t2: p2.set_clip_path(visible[0]) plt.show() # If you are using a Jupyter notebook you do not need this line .. image-sg:: /gallery/pitch_plots/images/sphx_glr_plot_voronoi_001.png :alt: plot voronoi :srcset: /gallery/pitch_plots/images/sphx_glr_plot_voronoi_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.312 seconds) .. _sphx_glr_download_gallery_pitch_plots_plot_voronoi.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_voronoi.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_voronoi.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_voronoi.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_