{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Pitch Visualisations\n\nFirst we import the Pitch classes and matplotlib\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom mplsoccer import Pitch, VerticalPitch"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Draw a pitch on a new axis\nLet's plot on a new axis first.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch()\n# specifying figure size (width, height)\nfig, ax = pitch.draw(figsize=(8, 4))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Draw on an existing axis\nmplsoccer also plays nicely with other matplotlib figures. To draw a pitch on an\nexisting matplotlib axis specify an ``ax`` in the ``draw`` method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, axs = plt.subplots(nrows=1, ncols=2)\npitch = Pitch()\npie = axs[0].pie(x=[5, 15])\npitch.draw(ax=axs[1])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Supported data providers\nmplsoccer supports 9 pitch types by specifying the ``pitch_type`` argument:\n'statsbomb', 'opta', 'tracab', 'wyscout', 'uefa', 'metricasports', 'custom',\n'skillcorner' and 'secondspectrum'.\nIf you are using tracking data or the custom pitch ('metricasports', 'tracab',\n'skillcorner', 'secondspectrum' or 'custom'), you also need to specify the\n``pitch_length`` and ``pitch_width``, which are typically 105 and 68 respectively.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(pitch_type='opta')  # example plotting an Opta/ Stats Perform pitch\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(pitch_type='tracab',  # example plotting a tracab pitch\n              pitch_length=105, pitch_width=68,\n              axis=True, label=True)  # showing axis labels is optional\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Adjusting the plot layout\nmplsoccer also plots on grids by specifying nrows and ncols.\nThe default is to use\ntight_layout. See: https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch()\nfig, axs = pitch.draw(nrows=2, ncols=3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "But you can also use constrained layout\nby setting ``constrained_layout=True`` and ``tight_layout=False``, which may look better.\nSee: https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch()\nfig, axs = pitch.draw(nrows=2, ncols=3, tight_layout=False, constrained_layout=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you want more control over how pitches are placed\nyou can use the grid method. This also works for one pitch (nrows=1 and ncols=1).\nIt also plots axes for an endnote and title (see the plot_grid example for more information).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch()\nfig, axs = pitch.grid(nrows=3, ncols=3, figheight=10,\n                      # the grid takes up 71.5% of the figure height\n                      grid_height=0.715,\n                      # 5% of grid_height is reserved for space between axes\n                      space=0.05,\n                      # centers the grid horizontally / vertically\n                      left=None, bottom=None)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Pitch orientation\nThere are four basic pitch orientations.\nTo get vertical pitches use the VerticalPitch class.\nTo get half pitches use the half=True argument.\n\nHorizontal full\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(half=False)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Vertical full\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = VerticalPitch(half=False)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Horizontal half\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(half=True)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Vertical half\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = VerticalPitch(half=True)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can also adjust the pitch orientations with the ``pad_left``, ``pad_right``,\n``pad_bottom`` and ``pad_top`` arguments to make arbitrary pitch shapes.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = VerticalPitch(half=True,\n                      pad_left=-10,  # bring the left axis in 10 data units (reduce the size)\n                      pad_right=-10,  # bring the right axis in 10 data units (reduce the size)\n                      pad_top=10,  # extend the top axis 10 data units\n                      pad_bottom=20)  # extend the bottom axis 20 data units\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Pitch appearance\nThe pitch appearance is adjustable.\nUse ``pitch_color`` and ``line_color``, and ``stripe_color`` (if ``stripe=True``)\nto adjust the colors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(pitch_color='#aabb97', line_color='white',\n              stripe_color='#c2d59d', stripe=True)  # optional stripes\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Line style\nThe pitch line style is adjustable.\nUse ``linestyle`` and ``goal_linestyle`` to adjust the colors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(linestyle='--', linewidth=1, goal_linestyle='-')\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Line alpha\nThe pitch transparency is adjustable.\nUse ``pitch_alpha`` and ``goal_alpha`` to adjust the colors.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(line_alpha=0.5, goal_alpha=0.3)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Corner arcs\nYou can add corner arcs to the pitch by setting ``corner_arcs`` = True\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = VerticalPitch(corner_arcs=True, half=True)\nfig, ax = pitch.draw(figsize=(10, 7.727))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Juego de Posici\u00f3n\nYou can add the Juego de Posici\u00f3n pitch lines and shade the middle third\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(positional=True, shade_middle=True, positional_color='#eadddd', shade_color='#f2f2f2')\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "mplsoccer can also plot grass pitches by setting ``pitch_color='grass'``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(pitch_color='grass', line_color='white',\n              stripe=True)  # optional stripes\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Three goal types are included ``goal_type='line'``, ``goal_type='box'``,\nand ``goal_type='circle'``\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, axs = plt.subplots(nrows=3, figsize=(10, 18))\npitch = Pitch(goal_type='box', goal_alpha=1)  # you can also adjust the transparency (alpha)\npitch.draw(axs[0])\npitch = Pitch(goal_type='line')\npitch.draw(axs[1])\npitch = Pitch(goal_type='circle', linewidth=1)\npitch.draw(axs[2])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The line markings and spot size can be adjusted via ``linewidth`` and ``spot_scale``.\nSpot scale also adjusts the size of the circle goal posts.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(linewidth=3,\n              # the size of the penalty and center spots relative to the pitch_length\n              spot_scale=0.01)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you need to lift the pitch markings above other elements of the chart.\nYou can do this via ``line_zorder``, ``stripe_zorder``,\n``positional_zorder``, and ``shade_zorder``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(line_zorder=2)  # e.g. useful if you want to plot pitch lines over heatmaps\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Axis\nBy default mplsoccer turns of the axis (border), ticks, and labels.\nYou can use them by setting the ``axis``, ``label`` and ``tick`` arguments.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pitch = Pitch(axis=True, label=True, tick=True)\nfig, ax = pitch.draw()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## xkcd\nFinally let's use matplotlib's xkcd theme.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.xkcd()\npitch = Pitch(pitch_color='grass', stripe=True)\nfig, ax = pitch.draw(figsize=(8, 4))\nannotation = ax.annotate('Who can resist this?', (60, 10), fontsize=30, ha='center')\n\nplt.show()  # If you are using a Jupyter notebook you do not need this line"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}