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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file SoccerViz-0.1.1.tar.gz.

File metadata

  • Download URL: SoccerViz-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for SoccerViz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c592116f40144db7da282d1b41e2f700eae9df4af042a967a566dbb5507aaf91
MD5 9110aaa8b52f04ec8ba640d15f8c74c7
BLAKE2b-256 2dc54aebda4c61d042706c8ad4aa445f6a1ecfdaa0d3f2e85d702ee4e8655954

See more details on using hashes here.

File details

Details for the file SoccerViz-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: SoccerViz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for SoccerViz-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63c272a1e2a70199c3dc255c38c6e98fb0cbbf0061b6e591906aa754fd6325e0
MD5 faace0d7653963a48d948bda6bcd5930
BLAKE2b-256 725d7a17b3b88c9bdc410d77785d598a5f1a187ced15ecdb9d8fdeb855858149

See more details on using hashes here.

Supported by

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