-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal Third Enrty Rate + Bar Chart.py
87 lines (70 loc) · 2.97 KB
/
Final Third Enrty Rate + Bar Chart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from selenium import webdriver
chrome_driver_path = r'C:\Users\shivank\Untitled Folder\chromedriver.exe'
data = pd.read_csv('LaLigaPass.csv')
data['x'] = data['x']*1.2
data['y'] = data['y']*0.8
data['endX'] = data['endX']*1.2
data['endY'] = data['endY']*0.8
def final_third_percentage(df1, team_id):
df2 = df1[df1['team_id'] == team_id]
df2 = df2.reset_index()
n = 0
completions = 0
total = 0
on = True
while on:
on = False
for i in range(n, len(df2['x'])):
if n > len(df2['x']):
break
if (df2['x'][i] < 6) and (30 < df2['y'][i] < 50) and (df2['endX'][i] < 18) and (18 < df2['endY'][i] < 62):
n = i + 2
on = True
break
if (df2['x'][i] < 6) and (df2['y'][i] > 30) and (df2['y'][i] < 50):
total += 1
for x in range(i, len(df2['x'])):
if df2['outcome'][x] != 'Unsuccessful' and df2['endX'][x] < 80:
continue
if df2['outcome'][x] != 'Unsuccessful' and df2['endX'][x] > 80:
completions += 1
n = x+1
on = True
break
break
percentage = (completions/total)*100
return percentage
teams = data['team_id'].unique().tolist()
driver.get('https://1xbet.whoscored.com/Teams/60/Show/Spain-Deportivo-Alaves')
names = []
for team in teams:
driver.get('https://1xbet.whoscored.com/Teams/{}/Show/Spain-Deportivo-Alaves'.format(team))
element = driver.find_element_by_css_selector('.team-header-name')
names.append(element.text)
driver.close()
comparison = [final_third_percentage(data, team) for team in teams]
c_list = zip(comparison, teams, names)
c_list = sorted(c_list, key=lambda x: x[0], reverse=True)
names = [c[2] for c in c_list]
rank = [c[0] for c in c_list]
fig, ax = plt.subplots(figsize=(18,12),facecolor='#222222')
ax.set_facecolor('#222222')
ax.barh(names, rank, color='red')
ax.margins(y=0)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_xlabel('% Of Final Third Entries From Uninterrupted Short Goal Kick Sequences',c='white',size=17,fontfamily='serif',labelpad=20,ha='center')
ax.set_yticklabels(names,size=17,fontfamily='serif',color='white')
ax.set_xticklabels([0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20], size=13, fontfamily='serif', color='white')
ax.tick_params(axis='y', size=0)
ax.tick_params(axis='x', size=0)
ax.invert_yaxis()
ax.text(-3, -1.5, ' Which La Liga Teams Had The Highest Final Third Entry Rates From Short Goal Kick Sequences?', color='white',size=22,fontweight='bold',fontfamily='serif')
ax.text(7, -1, 'La Liga 20/21', color='white',size=20, fontweight='bold', fontfamily='serif')
plt.tight_layout()
plt.savefig("final third entries", dpi=500, bbox_inches="tight")