Skip to main content

A Package to allow analyzing soccer event data easily

Project description

Installation

Use the package manager pip to install

    pip install SoccerViz==0.1.0

Import the necessary Libraries and SoccerViz Package

import re
import json
import pandas as pd
import requests
from bs4 import BeautifulSoup
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
import matplotlib.patheffects as path_effects
from mplsoccer import VerticalPitch, Pitch, FontManager
from highlight_text import ax_text

#SoccerViz Package
from SoccerViz import plot,extract,datafilter

Scrape event data from WhoScored ONLY inorder to use it for data analysis and visualization, you would have to fill in the following parameters according to your liking, below example is given follow it and keep in mind the instructions given too with the code.

#This is an example URL from Whoscored.com similar to the below one

url = 'https://www.whoscored.com/Matches/1729462/Live/England-Premier-League-2023-2024-Arsenal-Liverpool'  #This is an example URL from Whoscored.com
url_shots = 'https://api.sofascore.com/api/v1/event/11352376/shotmap'  
#This^ is taken from the Sofascore API you can do the same by watching Mckay Johns Tutorial on how to fetch API of shotmaps from Sofascore.

#Put your USER AGENT in the HEADERS parameter, you can find yours on "https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending"

HEADERS = {
        'User-Agent': "#your user agent"
    }

headers_shots = { 'Accept': '*/*',
'Sec-Fetch-Site': 'same-site',
'Accept-Language': 'en-GB,en;q=0.9',
# 'Accept-Encoding': 'gzip, deflate, br',
'Sec-Fetch-Mode': 'cors',
'Cache-Control': 'max-age=0',
'Origin': 'https://www.sofascore.com',
'User-Agent': "same user agent used in normal Headers", 
'Connection': 'keep-alive',
'Referer': 'https://www.sofascore.com/',
'Host': 'api.sofascore.com',
'Sec-Fetch-Dest': 'empty',
  }   

shots = requests.get(url_shots, headers=headers_shots)

#Call the extract functions to get Pass Dataframe,Players Dataframe of the particular match


df = extract.pass_data(url,HEADERS)
players_df = extract.player_data(url,HEADERS)

#You can find the TEAM Id's of the clubs on their WhoScored page html tags by clicking on the club logos

home_team_id=13 #Arsenal Team ID
away_team_id=26 #Liverpool Team ID

Download the EPL Club data excel file which is present in the repository and assign df_clubs to the file location

df_clubs = pd.read_excel('#file_local_location')

After scrapping the event data and assembling into DataFrames, you will need to filter the data according to the teams and players.

#Filter all the data according to the teams by calling the function into DataFrames(their names are pretty self explanatory)

#You can use and call any dataframe you would like to analyze in raw tables and columns i.e. in a DataFrame form
pass_between_home, pass_between_away, avg_loc_home, avg_loc_away, passes_home, passes_away,df_prg_home,df_comp_prg_home,df_uncomp_prg_home,df_prg_away,df_comp_prg_away,df_uncomp_prg_away = datafilter.analyze_passes(df, players_df, home_team_id, away_team_id)

#Same goes for the shots of the match
df1_missed,df2_missed,df1_saved,df2_saved,df1_goal,df2_goal,df1_block,df2_block,totalxG1,totalxG2=datafilter.analyze_shots(shots)

Now finally, you can plot the pass network map for both the teams to analyze and visualize by calling the function

#Call the function and manually put in the home and away team names

plot = plot.pass_network(pass_between_home, pass_between_away, avg_loc_home,avg_loc_away,hometeam_name,awayteam_name)

test1.png

Plotting Prg Passes

#Call the function and put in home and away team names
plot = plot.prg_passes(df_comp_prg_home, df_uncomp_prg_home, df_comp_prg_away, df_uncomp_prg_away, hometeam_name,
                awayteam_name)

test.png

Plotting Shot Maps

#Call the function 
plot = plot.shot_map(df1_missed, df2_missed, df1_saved, df2_saved, df1_goal, df2_goal, df1_block, df2_block, home_team_id,
             away_team_id, df_clubs, totalxG1, totalxG2)

test4.png

Credits

Huge Shoutout to the guys at Mplsoccer, do check their package out also, and also checkout Mckay Johns Youtube Channel, which helped me alot in learning python and football analytics

And please don't forget to drop me feedback on Twitter/X, @athalakbar13.

Enjoy!!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SoccerViz-0.1.1.tar.gz (10.0 kB view hashes)

Uploaded Source

Built Distribution

SoccerViz-0.1.1-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page