image_output_1.png
from matplotlib.pylab import rcParams

import matplotlib.gridspec as gridspec

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('/kaggle/input/playoff_shots.csv')

def shooting_summary(df, by):
    df_summary = df.copy()
    df_3p = df_summary[df_summary['shot_type'] == '3PT']
    df_2p = df_summary[df_summary['shot_type'] == '2PT']
    df_ft = df_summary[df_summary['shot_type'] == 'FT']
    result = pd.DataFrame()
    all_make = df_summary[df_summary['result'] == 'make'].groupby(by).count()['game_id']
    all_miss = df_summary[df_summary['result'] == 'miss'].groupby(by).count()['game_id']
    result['Shot Attempts'] = all_make + all_miss
    result['Shot %'] = all_make / result['Shot Attempts'] * 100
    make_3p = df_3p[df_3p['result'] == 'make'].groupby(by).count()['game_id']
    miss_3p = df_3p[df_3p['result'] == 'miss'].groupby(by).count()['game_id']
    result['3p Attempts'] = make_3p + miss_3p
    result['3p %'] = make_3p / result['3p Attempts'] * 100
    make_2p = df_2p[df_2p['result'] == 'make'].groupby(by).count()['game_id']
    miss_2p = df_2p[df_2p['result'] == 'miss'].groupby(by).count()['game_id']
    result['2p Attempts'] = make_2p + miss_2p
    result['2p %'] = make_2p / result['2p Attempts'] * 100
    make_ft = df_ft[df_ft['result'] == 'make'].groupby(by).count()['game_id']
    miss_ft = df_ft[df_ft['result'] == 'miss'].groupby(by).count()['game_id']
    result['FT Attempts'] = make_ft + miss_ft
    result['FT %'] = make_ft / result['FT Attempts'] * 100
    result['TS Proportion'] = result['Shot Attempts'] / np.sum(result['Shot Attempts']) * 100
    return result

### Shooting summary of GSW
rcParams['figure.figsize'] = [15,5]
plt.style.use('fivethirtyeight') 
sns.set_style('whitegrid')
grid = gridspec.GridSpec(1,2)

# Plot prep
gsw = df[df['team']=='Golden State Warriors']
df_plot = shooting_summary(gsw, 'player_name').fillna(0).sort_values('Shot Attempts', ascending=False).head(5)

# Plot shooting percentage
ax1 = plt.subplot(grid[0])
sns.heatmap(df_plot[['Shot %', '3p %', '2p %', 'FT %', 'TS Proportion']], annot=True, ax=ax1, cmap='Pastel1_r', linewidths=2)
ax1.set_title('GSW Shooting Percentage', fontname='Monospace', fontsize='20', fontweight='bold')

# Plot shooting attempts
ax2 = plt.subplot(grid[1])
sns.heatmap(df_plot[['3p Attempts', '2p Attempts', 'FT Attempts']], annot=True, fmt='.2f', cmap='Pastel1_r', ax=ax2, linewidths=2)
ax2.set_title('GSW Shooting Attempts', fontname='Monospace', fontsize='20', fontweight='bold')
plt.tight_layout()

rcParams['figure.figsize'] = [10, 5]
plt.style.use('fivethirtyeight')
sns.set_style('whitegrid')
kevin_durant_prop = 26.2757
result = pd.DataFrame()
for team in df['team'].unique():
    df_team = df[df['team'] == team]
    summary = shooting_summary(df_team, 'player_name')
    bool_team_shot = summary['TS Proportion'] >= kevin_durant_prop
    result = pd.concat([result, summary[bool_team_shot]])
cmap = sns.color_palette('Pastel1_r')
result['TS Proportion'].sort_values().plot(kind='barh', color=cmap)
list_x = list(np.round(result['TS Proportion'].sort_values(), 2))
list_y = list(np.arange(-0.1, 5.9, 1))
for x, y in zip(list_x, list_y):
    plt.text(x + 0.5, y, str(x), fontweight='bold', fontsize=14, fontname='Monospace')
plt.text(np.max(list_x) + 0.5, 0.4, 'Threshold', fontname='Monospace', fontsize=14)
plt.xlim(0, np.max(list_x) * 1.2)

def highlight_max(s):
    """
    highlight the maximum 
    """
    is_max = s == s.max()
    return ['background-color:#f85a40' if v else '' for v in is_max]
result[result.index.isin(['LeBron James', 'Kevin Durant'])].T.style.apply(highlight_max, axis=1)

df_final = df[df['game_description'].str.contains('CLE v GS')]
result = player_shooting_summary(df_final)
result[result.index.isin(['LeBron James', 'Kevin Durant'])].T.style.apply(highlight_max, axis=1)

rcParams['figure.figsize'] = [10, 5]
plt.style.use('fivethirtyeight')
sns.set_style('whitegrid')
list_var = ['Shot %', '3p %', '2p %', 'FT %']
team = 'Golden State Warriors'
result = pd.DataFrame()
first_round_desc = list(df['game_description'].unique())[:44]
first_round_desc.append('CLE v IND, G7')
first_round = df[df['game_description'].isin(first_round_desc)]
summary_first_round = shooting_summary(first_round, 'team')[list_var]
for var in list_var:
    summary_first_round[var] = summary_first_round[var].rank(ascending=False)
team_summary = summary_first_round[summary_first_round.index == team]
team_summary.index = ['First Round (16)']
result = pd.concat([result, team_summary])
conf_semi_desc = list(df['game_description'].unique())[44:64]
conf_semi_desc.remove('CLE v IND, G7')
conf_semi = df[df['game_description'].isin(conf_semi_desc)]
summary_conf_semi = shooting_summary(conf_semi, 'team')[list_var]
for var in list_var:
    summary_conf_semi[var] = summary_conf_semi[var].rank(ascending=False, method='min')
team_summary = summary_conf_semi[summary_conf_semi.index == team]
team_summary.index = ['Conf. Semifinals (8)']
result = pd.concat([result, team_summary])
conf_finals_desc = list(df['game_description'].unique())[64:78]
conf_finals = df[df['game_description'].isin(conf_finals_desc)]
summary_conf_finals = shooting_summary(conf_finals, 'team')[list_var]
for var in list_var:
    summary_conf_finals[var] = summary_conf_finals[var].rank(ascending=False)
team_summary = summary_conf_finals[summary_conf_finals.index == team]
team_summary.index = ['Conf. Finals (4)']
result = pd.concat([result, team_summary])
finals_desc = list(df['game_description'].unique())[78:]
finals = df[df['game_description'].isin(finals_desc)]
summary_finals = shooting_summary(finals, 'team')[list_var]
for var in list_var:
    summary_finals[var] = summary_finals[var].rank(ascending=False)
team_summary = summary_finals[summary_finals.index == team]
team_summary.index = ['Finals (2)']
result = pd.concat([result, team_summary])

first_round_desc = list(df['game_description'].unique())[:44]
first_round_desc.append('CLE v IND, G7')
first_round = df[df['game_description'].isin(first_round_desc)]
shooting_summary(first_round, 'team').sort_values('2p %', ascending=False).style.apply(highlight_max)