-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam_TAA_table.py
139 lines (118 loc) · 4.02 KB
/
team_TAA_table.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from plottable import ColumnDefinition, Table
from plottable.cmap import normed_cmap
from plottable.plots import image, circled_image, bar
from plottable.formatters import decimal_to_percent
import os
from dotenv import load_dotenv
load_dotenv()
current_directory = os.environ["current_directory"]
teams = pd.read_csv("data/teams.csv")
teams_TAA = teams[teams['name'] != 'FB']
teams_TAA['logo'] = teams_TAA['name'].apply(
lambda x:
f"{current_directory}/team_logos/{x}_logo.png"
)
team_abbr_mapping = {
'ARI': 'Arizona Cardinals',
'ATL': 'Atlanta Falcons',
'BAL': 'Baltimore Ravens',
'BUF': 'Buffalo Bills',
'CAR': 'Carolina Panthers',
'CHI': 'Chicago Bears',
'CIN': 'Cincinnati Bengals',
'CLE': 'Cleveland Browns',
'DAL': 'Dallas Cowboys',
'DEN': 'Denver Broncos',
'DET': 'Detroit Lions',
'GB': 'Green Bay Packers',
'HOU': 'Houston Texans',
'IND': 'Indianapolis Colts',
'JAX': 'Jacksonville Jaguars',
'KC': 'Kansas City Chiefs',
'LV': 'Las Vegas Raiders',
'LAC': 'Los Angeles Chargers',
'LA': 'Los Angeles Rams',
'MIA': 'Miami Dolphins',
'MIN': 'Minnesota Vikings',
'NE': 'New England Patriots',
'NO': 'New Orleans Saints',
'NYG': 'New York Giants',
'NYJ': 'New York Jets',
'PHI': 'Philadelphia Eagles',
'PIT': 'Pittsburgh Steelers',
'SEA': 'Seattle Seahawks',
'SF': 'San Francisco 49ers',
'TB': 'Tampa Bay Buccaneers',
'TEN': 'Tennessee Titans',
'WAS': 'Washington Commanders'
}
teams_TAA['full_team_name'] = teams_TAA['name'].map(team_abbr_mapping)
teams_TAA.rename(columns = {'tackles_above_expected': 'tackles_above_average'}, inplace = True)
teams_TAA = teams_TAA.sort_values(by = 'tackles_above_average', ascending = False)
teams_TAA = teams_TAA.drop('id', axis = 1)
teams_TAA = teams_TAA.reset_index(drop = True)
teams_TAA.index += 1
teams_TAA['Rank'] = teams_TAA.index
teams_TAA = teams_TAA[['Rank', 'logo', 'full_team_name', 'tackles_above_average']]
bg_color = "#FFFFFF"
text_color = "#000000"
row_colors = {"#91C465", "D0F0C0", "F0FFF0", "F5FFFA"}
plt.rcParams['text.color'] = text_color
plt.rcParams['font.family'] = "monospace"
cmap = normed_cmap(teams_TAA["tackles_above_average"], cmap = plt.cm.PiYG, num_stds = 2.5)
def plot_total_TAA_bar(ax, val, height, cmap, width = 0.5):
color = cmap(val)
ax.barh(y=[0], width=[val], color=color, height=height)
ax.set_xlim(-15.5, 15)
ax.axis('off')
ax.text(val if val >= 0 else val - 0.5, 0, f'{val:.2f}',
va='center', ha='right' if val < 0 else 'left', fontweight='bold')
col_defs = [
ColumnDefinition(
name = "Rank",
textprops = {"ha": "center", "weight":"bold"},
width = 0.1
),
ColumnDefinition(
name = "logo",
title = "Team",
textprops = {"ha": "center", "va":"center", "weight":"bold", "color": text_color},
width = 0.1,
plot_fn = image
),
ColumnDefinition(
name = "full_team_name",
title = "",
textprops = {"ha": "left", "va":"center", "weight":"bold", "color": text_color},
width = 0.4,
),
ColumnDefinition(
name = "tackles_above_average",
title = "Total Tackles Above Average (TAA)",
textprops = {"ha": "center", "va":"center", "weight":"bold", "color": text_color},
width = 1.25,
plot_fn = plot_total_TAA_bar,
plot_kw = {
"height": 0.5,
"cmap": cmap
}
)
]
fig, ax = plt.subplots(figsize=(20,22))
fig.set_facecolor(bg_color)
ax.set_facecolor(bg_color)
table = Table(
teams_TAA,
column_definitions = col_defs,
index_col = "Rank",
row_dividers = True,
row_divider_kw = {"linewidth":1, "linestyle": (0, (1,5))},
footer_divider = True,
textprops = {"fontsize":14},
ax = ax
).autoset_fontcolors(colnames=["tackles_above_average"])
fig.savefig('Figures/team_TAA.png', facecolor = ax.get_facecolor(), dpi = 400)
plt.show()