.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch_setup/plot_pitches.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_setup_plot_pitches.py: ============ Pitch Basics ============ First we import the Pitch classes and matplotlib .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python import matplotlib.pyplot as plt from mplsoccer import Pitch, VerticalPitch .. GENERATED FROM PYTHON SOURCE LINES 13-16 Draw a pitch on a new axis -------------------------- Let's plot on a new axis first. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python pitch = Pitch() # specifying figure size (width, height) fig, ax = pitch.draw(figsize=(8, 4)) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_001.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 22-26 Draw on an existing axis ------------------------ mplsoccer also plays nicely with other matplotlib figures. To draw a pitch on an existing matplotlib axis specify an ``ax`` in the ``draw`` method. .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python fig, axs = plt.subplots(nrows=1, ncols=2) pitch = Pitch() pie = axs[0].pie(x=[5, 15]) pitch.draw(ax=axs[1]) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_002.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 33-41 Supported data providers ------------------------ mplsoccer supports 10 pitch types by specifying the ``pitch_type`` argument: 'statsbomb', 'opta', 'tracab', 'wyscout', 'uefa', 'metricasports', 'custom', 'skillcorner', 'secondspectrum' and 'impect'. If you are using tracking data or the custom pitch ('metricasports', 'tracab', 'skillcorner', 'secondspectrum' or 'custom'), you also need to specify the ``pitch_length`` and ``pitch_width``, which are typically 105 and 68 respectively. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python pitch = Pitch(pitch_type='opta') # example plotting an Opta/ Stats Perform pitch fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_003.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python pitch = Pitch(pitch_type='tracab', # example plotting a tracab pitch pitch_length=105, pitch_width=68, axis=True, label=True) # showing axis labels is optional fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_004.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-58 Adjusting the plot layout ------------------------- mplsoccer also plots on grids by specifying nrows and ncols. The default is to use tight_layout. See: https://matplotlib.org/stable/users/explain/axes/tight_layout_guide.html. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python pitch = Pitch() fig, axs = pitch.draw(nrows=2, ncols=3) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_005.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-66 But you can also use constrained layout by setting ``constrained_layout=True`` and ``tight_layout=False``, which may look better. See: https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: Python pitch = Pitch() fig, axs = pitch.draw(nrows=2, ncols=3, tight_layout=False, constrained_layout=True) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_006.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-74 If you want more control over how pitches are placed you can use the grid method. This also works for one pitch (nrows=1 and ncols=1). It also plots axes for an endnote and title (see the plot_grid example for more information). .. GENERATED FROM PYTHON SOURCE LINES 74-84 .. code-block:: Python pitch = Pitch() fig, axs = pitch.grid(nrows=3, ncols=3, figheight=10, # the grid takes up 71.5% of the figure height grid_height=0.715, # 5% of grid_height is reserved for space between axes space=0.05, # centers the grid horizontally / vertically left=None, bottom=None) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_007.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-92 Pitch orientation ----------------- There are four basic pitch orientations. To get vertical pitches use the VerticalPitch class. To get half pitches use the half=True argument. Horizontal full .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. code-block:: Python pitch = Pitch(half=False) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_008.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-98 Vertical full .. GENERATED FROM PYTHON SOURCE LINES 98-102 .. code-block:: Python pitch = VerticalPitch(half=False) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_009.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 103-104 Horizontal half .. GENERATED FROM PYTHON SOURCE LINES 104-107 .. code-block:: Python pitch = Pitch(half=True) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_010.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 108-109 Vertical half .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python pitch = VerticalPitch(half=True) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_011.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-115 You can also adjust the pitch orientations with the ``pad_left``, ``pad_right``, ``pad_bottom`` and ``pad_top`` arguments to make arbitrary pitch shapes. .. GENERATED FROM PYTHON SOURCE LINES 115-123 .. code-block:: Python pitch = VerticalPitch(half=True, pad_left=-10, # bring the left axis in 10 data units (reduce the size) pad_right=-10, # bring the right axis in 10 data units (reduce the size) pad_top=10, # extend the top axis 10 data units pad_bottom=20) # extend the bottom axis 20 data units fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_012.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 124-129 Pitch appearance ---------------- The pitch appearance is adjustable. Use ``pitch_color`` and ``line_color``, and ``stripe_color`` (if ``stripe=True``) to adjust the colors. .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: Python pitch = Pitch(pitch_color='#aabb97', line_color='white', stripe_color='#c2d59d', stripe=True) # optional stripes fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_013.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_013.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 135-139 Line style ---------- The pitch line style is adjustable. Use ``linestyle`` and ``goal_linestyle`` to adjust the colors. .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: Python pitch = Pitch(linestyle='--', linewidth=1, goal_linestyle='-') fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_014.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_014.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 144-148 Line alpha ---------- The pitch transparency is adjustable. Use ``pitch_alpha`` and ``goal_alpha`` to adjust the colors. .. GENERATED FROM PYTHON SOURCE LINES 148-152 .. code-block:: Python pitch = Pitch(line_alpha=0.5, goal_alpha=0.3) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_015.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_015.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 153-156 Corner arcs ----------- You can add corner arcs to the pitch by setting ``corner_arcs`` = True .. GENERATED FROM PYTHON SOURCE LINES 156-160 .. code-block:: Python pitch = VerticalPitch(corner_arcs=True, half=True) fig, ax = pitch.draw(figsize=(10, 7.727)) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_016.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_016.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 161-165 Juego de Posición ----------------- You can add the Juego de Posición pitch lines and shade the middle third. You can also adjust the transparency via ``shade_alpha`` and ``positional_alpha``. .. GENERATED FROM PYTHON SOURCE LINES 165-169 .. code-block:: Python pitch = Pitch(positional=True, shade_middle=True, positional_color='#eadddd', shade_color='#f2f2f2') fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_017.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_017.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 170-171 mplsoccer can also plot grass pitches by setting ``pitch_color='grass'``. .. GENERATED FROM PYTHON SOURCE LINES 171-176 .. code-block:: Python pitch = Pitch(pitch_color='grass', line_color='white', stripe=True) # optional stripes fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_018.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_018.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 177-179 Three goal types are included ``goal_type='line'``, ``goal_type='box'``, and ``goal_type='circle'`` .. GENERATED FROM PYTHON SOURCE LINES 179-188 .. code-block:: Python fig, axs = plt.subplots(nrows=3, figsize=(10, 18)) pitch = Pitch(goal_type='box', goal_alpha=1) # you can also adjust the transparency (alpha) pitch.draw(axs[0]) pitch = Pitch(goal_type='line') pitch.draw(axs[1]) pitch = Pitch(goal_type='circle', linewidth=1) pitch.draw(axs[2]) .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_019.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_019.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 189-191 The line markings and spot size can be adjusted via ``linewidth`` and ``spot_scale``. Spot scale also adjusts the size of the circle goal posts. .. GENERATED FROM PYTHON SOURCE LINES 191-197 .. code-block:: Python pitch = Pitch(linewidth=3, # the size of the penalty and center spots relative to the pitch_length spot_scale=0.1) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_020.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_020.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-199 The center and penalty spots can also be changed to a square to avoid clashes with scatter points. .. GENERATED FROM PYTHON SOURCE LINES 199-203 .. code-block:: Python pitch = Pitch(spot_type='square', spot_scale=0.1) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_021.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_021.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 204-207 If you need to lift the pitch markings above other elements of the chart. You can do this via ``line_zorder``, ``stripe_zorder``, ``positional_zorder``, and ``shade_zorder``. .. GENERATED FROM PYTHON SOURCE LINES 207-211 .. code-block:: Python pitch = Pitch(line_zorder=2) # e.g. useful if you want to plot pitch lines over heatmaps fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_022.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_022.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 212-216 Axis ---- By default mplsoccer turns of the axis (border), ticks, and labels. You can use them by setting the ``axis``, ``label`` and ``tick`` arguments. .. GENERATED FROM PYTHON SOURCE LINES 216-220 .. code-block:: Python pitch = Pitch(axis=True, label=True, tick=True) fig, ax = pitch.draw() .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_023.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_023.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 221-224 xkcd ---- Finally let's use matplotlib's xkcd theme. .. GENERATED FROM PYTHON SOURCE LINES 224-231 .. code-block:: Python plt.xkcd() pitch = Pitch(pitch_color='grass', stripe=True) fig, ax = pitch.draw(figsize=(8, 4)) annotation = ax.annotate('Who can resist this?', (60, 10), fontsize=30, ha='center') plt.show() # If you are using a Jupyter notebook you do not need this line .. image-sg:: /gallery/pitch_setup/images/sphx_glr_plot_pitches_024.png :alt: plot pitches :srcset: /gallery/pitch_setup/images/sphx_glr_plot_pitches_024.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.936 seconds) .. _sphx_glr_download_gallery_pitch_setup_plot_pitches.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pitches.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pitches.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_pitches.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_