Plotting¶
In this quickstart, we'll show all kind of examples. As PySport encourages OpenSource projects, all examples use other sports analytics packages in combination with kloppy. You can find more packages at https://opensource.pysport.org/
Plotting events using mplsoccer¶
In this example the mplsoccer package by Andy Rowlinson is used.
In [1]:
Copied!
import sys
!{sys.executable} -m pip install mplsoccer matplotlib seaborn
import sys
!{sys.executable} -m pip install mplsoccer matplotlib seaborn
Requirement already satisfied: mplsoccer in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (1.1.10) Requirement already satisfied: matplotlib in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (3.7.1) Requirement already satisfied: seaborn in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (0.12.2) Requirement already satisfied: pandas in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from mplsoccer) (2.0.3) Requirement already satisfied: numpy in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from mplsoccer) (1.24.2) Requirement already satisfied: pillow in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from mplsoccer) (9.4.0) Requirement already satisfied: scipy in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from mplsoccer) (1.10.1) Requirement already satisfied: requests in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/requests-2.28.2-py3.10.egg (from mplsoccer) (2.28.2) Requirement already satisfied: cycler>=0.10 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (0.11.0) Requirement already satisfied: pyparsing>=2.3.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (3.0.9) Requirement already satisfied: contourpy>=1.0.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (1.0.7) Requirement already satisfied: python-dateutil>=2.7 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/python_dateutil-2.8.2-py3.10.egg (from matplotlib) (2.8.2) Requirement already satisfied: packaging>=20.0 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (23.0) Requirement already satisfied: kiwisolver>=1.0.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (1.4.4) Requirement already satisfied: fonttools>=4.22.0 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from matplotlib) (4.38.0) Requirement already satisfied: tzdata>=2022.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages (from pandas->mplsoccer) (2023.3) Requirement already satisfied: pytz>=2020.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/pytz-2022.7.1-py3.10.egg (from pandas->mplsoccer) (2022.7.1) Requirement already satisfied: six>=1.5 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/six-1.16.0-py3.10.egg (from python-dateutil>=2.7->matplotlib) (1.16.0) Requirement already satisfied: charset-normalizer<4,>=2 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/charset_normalizer-3.0.1-py3.10-macosx-12-arm64.egg (from requests->mplsoccer) (3.0.1) Requirement already satisfied: idna<4,>=2.5 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/idna-3.4-py3.10.egg (from requests->mplsoccer) (3.4) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/urllib3-1.26.14-py3.10.egg (from requests->mplsoccer) (1.26.14) Requirement already satisfied: certifi>=2017.4.17 in /Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/certifi-2022.12.7-py3.10.egg (from requests->mplsoccer) (2022.12.7) [notice] A new release of pip is available: 23.0.1 -> 24.0 [notice] To update, run: pip install --upgrade pip
In [2]:
Copied!
## Load data
from mplsoccer.pitch import Pitch
from kloppy import statsbomb
dataset = statsbomb.load_open_data(
event_types=["pass"],
coordinates="statsbomb"
)
home_team, away_team = dataset.metadata.teams
messi = home_team.players[9]
print(f"Going to show passes of: {messi}")
## Load data
from mplsoccer.pitch import Pitch
from kloppy import statsbomb
dataset = statsbomb.load_open_data(
event_types=["pass"],
coordinates="statsbomb"
)
home_team, away_team = dataset.metadata.teams
messi = home_team.players[9]
print(f"Going to show passes of: {messi}")
Going to show passes of: Lionel Andrés Messi Cuccittini
In [3]:
Copied!
df = (
dataset
.filter(lambda event: event.player == messi)
.to_df()
)
pitch = Pitch(pitch_color='#e7f1fa', line_zorder=1, line_color='black', pitch_type="statsbomb")
fig, ax = pitch.draw()
plot = pitch.kdeplot(
df["coordinates_x"],
df["coordinates_y"],
ax=ax,
fill=True,
n_levels=50,
)
df = (
dataset
.filter(lambda event: event.player == messi)
.to_df()
)
pitch = Pitch(pitch_color='#e7f1fa', line_zorder=1, line_color='black', pitch_type="statsbomb")
fig, ax = pitch.draw()
plot = pitch.kdeplot(
df["coordinates_x"],
df["coordinates_y"],
ax=ax,
fill=True,
n_levels=50,
)
In [ ]:
Copied!