Skip to content

Instantly share code, notes, and snippets.

@koenvo
Last active February 4, 2022 09:08
Show Gist options
  • Save koenvo/c450c2e08ad8f0c38bb3d183b2d65648 to your computer and use it in GitHub Desktop.
Save koenvo/c450c2e08ad8f0c38bb3d183b2d65648 to your computer and use it in GitHub Desktop.
Soccer analytics open-source demo
kloppy
mplsoccer
streamlit
natsort
import os
from glob import glob
from natsort import natsorted
from mplsoccer.pitch import Pitch
from kloppy import load_opta_event_data, to_pandas
import streamlit as st
match_list = [os.path.basename(filename) for filename in glob("data/*")]
match = st.sidebar.selectbox("Select match", match_list, index=0)
# make sure you have a directory called "data" with directories containing the f24/f7 xml files
f24_filename = f"data/{match}/f24.xml"
f7_filename = f"data/{match}/f7.xml"
dataset = load_opta_event_data(
f24_filename,
f7_filename,
options={"event_types": ["pass"]}
)
dataframe = to_pandas(dataset)
# This will create an unique Set of team/jersey no combos
players = {f"{event.team} - {event.player_jersey_no}" for event in dataset.events}
player = st.sidebar.selectbox("Select player", natsorted(players), index=0)
st.text(f"Selected match: {match} / Player: {player}")
team, jersey_no = player.split(" - ")
player_events = dataframe[(dataframe.team == team) & (dataframe.player_jersey_no == jersey_no)]
pitch = Pitch(pitch_color='#e7f1fa', line_zorder=1, line_color='black', pitch_type="opta")
fig, ax = pitch.draw()
pitch.kdeplot(
player_events["position_x"],
player_events["position_y"],
ax=ax,
shade=True,
n_levels=50,
)
st.pyplot()
# run using: streamlit run script.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment