After modelling both model of space occupation and comparing them, we focus on the correlation between space occupation and 3-point performance. To do so, we browse all the games we have, we track when there are 3-point shots and then memorize the evolution of pressure on the shooter 3 seconds before he shots. The algorithm is not presented here, but is available as a python file : track_shoot.py.
Here we extract the informations we want from the data we collected.
The shots are memorized in two csv files : df_plot_mean and df_shots calculated thanks to python_file shots_statistics.py. They contain the same information but aren't structured in the same way (df_plot_mean's structure let us plot averaged evolution). They are composed of the following columns :
import pandas as pd
import ast
df_plot_mean=pd.read_csv('data/df_plot_mean.csv',index_col=[0])
df_shots=pd.read_csv('data/df_shots.csv',index_col=[0],converters={1:ast.literal_eval,2:ast.literal_eval,3:ast.literal_eval,7:ast.literal_eval,8:ast.literal_eval,9:ast.literal_eval,18:ast.literal_eval,19:ast.literal_eval,20:ast.literal_eval})
df_stats=pd.read_csv('data/df_stats.csv',index_col=[0])
print('number of shots:',len(df_shots))
print(df_stats['total_cas'].sum(),df_stats['total_cas'].sum()/26295*100)
print(df_stats['success'].sum(),df_stats['miss'].sum(),df_stats['success'].sum()/26295*100)
print(df_stats['success_cas'].sum(),df_stats['miss_cas'].sum(),df_stats['success_cas'].sum()/18896*100)
print(len(df_shots.query('Shot_type=="pull-up 3P" and Shot_result==1'))/len(df_shots.query('Shot_type=="pull-up 3P"')))
number of shots: 26332 18916 71.93763072827534 9136 17196 34.74424795588515 6803 12113 36.00232853513971 0.3145900755124056
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(15,10))
for row in df_shots.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
plt.plot(Time,D,'k-',alpha=0.005)
plt.xlim((-3.2,0.8))
plt.ylim((0,30))
(0, 30)
First we compute a function to plot the evolution of free space averaged on all the shots. Let's note that we round the values of time with a precision of 0.01 second. Yet the precision of position data is only 0.04 seconds.
Let's note that we delet some shots because the values of time are wrong : the clock stops but the evolution continue to be captured therefore, if we don't do that, we have false values for a certain value of time.
def averaged_evolution(df,sd=None,hue=None,palette=['blue'],style=None,fontsize='medium',size=None):
## rename columns to have good labels ##
df2=df.rename(columns={"Time": "time [s]",'D':r'$<\delta_{d}^*(t)$> [feet]','T':r'$<\delta_{t}^*(t)$> [s]'})
### plot ###
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{d}^*(t)$> [feet]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize=fontsize)
plt.show()
plt.clf()
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{t}^*(t)$> [s]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize=fontsize)
plt.show()
plt.clf()
df_plot_mean=df_plot_mean.query('Time>-3. and Time<0.8')
averaged_evolution(df_plot_mean)
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_plot_mean)
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
We see that there is some noise. To calculate the plot sns.lineplot take each value of time and make a mean over all the pressure value associated. If we count the number of pressure values per time value we can see that there isn't an equilibrium as there are more of multiple of 0.04 :
print('count',df_plot_mean.groupby('Time').count())
count D T Time_to_shoot Shot result player_id x_ball y_ball \ Time -3.20 50 50 50 50 50 50 50 -3.19 1 1 1 1 1 1 1 -3.19 141 141 141 141 141 141 141 -3.18 189 189 189 189 189 189 189 -3.17 3 3 3 3 3 3 3 ... ... ... ... ... ... ... ... 0.79 3 3 3 3 3 3 3 0.79 677 677 677 677 677 677 677 0.79 1699 1699 1699 1699 1699 1699 1699 0.80 8 8 8 8 8 8 8 0.80 6238 6238 6238 6238 6238 6238 6238 z_ball x_shooter y_shooter quarter clock Match_id shot_id \ Time -3.20 50 50 50 50 50 50 50 -3.19 1 1 1 1 1 1 1 -3.19 141 141 141 141 141 141 141 -3.18 189 189 189 189 189 189 189 -3.17 3 3 3 3 3 3 3 ... ... ... ... ... ... ... ... 0.79 3 3 3 3 3 3 3 0.79 677 677 677 677 677 677 677 0.79 1699 1699 1699 1699 1699 1699 1699 0.80 8 8 8 8 8 8 8 0.80 6238 6238 6238 6238 6238 6238 6238 Shot_type btpl Time -3.20 50 50 -3.19 1 1 -3.19 141 141 -3.18 189 189 -3.17 3 3 ... ... ... 0.79 3 3 0.79 677 677 0.79 1699 1699 0.80 8 8 0.80 6238 6238 [1122 rows x 16 columns]
Our aim is to make mean for each value of time over the 26325 shots. To do so, we will aggregate values around multiple of 0.04 seconds with the following function :
def aggregating_04(row):
number=row['Time']
q=number//0.04
if abs(round((q*0.04-number),2))<=0.02:
return(q*0.04)
else :
return((q+1)*0.04)
df_plot_mean['Time']=df_plot_mean.apply(aggregating_04,axis=1)
averaged_evolution(df_plot_mean)
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
The result is better here. Now let's see if we plot the standard deviation :
averaged_evolution(df_plot_mean,sd='sd')
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_plot_mean,sd='sd')
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
We distinguish two types of shots :
In fact some shots can be in neither category : wait more than two seconds without dribbling. But we admit that this is only a small part of shots. Therefore to distinguish both types we look at the value TIME_TO_SHOOT.
We found that there were 78% of catch-and-shoot shots. Yet our games are from 2013 to 2016, according to https://www.thespax.com/nba/three-point-shooting-part-i-the-dying-catch-and-shoot-shot/, there were about 75% of catch-and-shoot shots. Our value is closed to the reality.
averaged_evolution(df_plot_mean,palette=['blue','orange'],hue='Shot_type')
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_plot_mean,palette=['blue','orange'],hue='Shot_type',sd='sd')
<Figure size 432x288 with 0 Axes>
We want to know if the pressure exerced on a player has an influence on his 3-point performance.
averaged_evolution(df_plot_mean,palette=['blue','orange'],hue='Shot_type',style='Shot result',fontsize='small')
<Figure size 432x288 with 0 Axes>
df_curry_mean=df_plot_mean.query('player_id==201939').copy()
averaged_evolution(df_curry_mean)
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_curry_mean,sd='sd')
No handles with labels found to put in legend.
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_curry_mean,palette=['blue','orange'],hue='Shot_type')
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_curry_mean,palette=['blue','orange'],hue='Shot_type',sd='sd')
<Figure size 432x288 with 0 Axes>
averaged_evolution(df_curry_mean,palette=['blue','orange'],hue='Shot_type',style='Shot result')
<Figure size 432x288 with 0 Axes>
def averaged_evolution_two_df(df,df_curry,sd=None,hue=None,palette=['blue'],style=None,size=None):
## rename columns to have good labels ##
df2=pd.concat([df,df_curry],ignore_index=True)
players=['all' for k in range(len(df))]+['Curry' for k in range(len(df_curry))]
df2['Player']=players
df2=df2.rename(columns={"Time": "time [s]",'D':r'$<\delta_{d}^*(t)$> [feet]','T':r'$<\delta_{t}^*(t)$> [s]'})
### plot ###
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{d}^*(t)$> [feet]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize='small')
plt.show()
plt.clf()
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{t}^*(t)$> [s]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize='small')
#plt.savefig('Comparison_curry_t',dpi=72)
plt.show()
plt.clf()
averaged_evolution_two_df(df_plot_mean,df_curry_mean,sd=None,hue='Shot_type',palette=['blue','orange'],style=None,size='Player')
<Figure size 432x288 with 0 Axes>
df_curry=df_shots.query('player_id==201939').copy()
fig, (ax1, ax2) = plt.subplots(1, 2,figsize=(20,7))
k=0
for row in df_curry.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax1.plot(Time,D,'k-',alpha=0.1)
ax2.plot(Time,T,'k-',alpha=0.1)
k+=1
ax1.set_xlim((-3.2,0.8))
ax1.set_ylim((0,30))
ax2.set_xlim((-3.2,0.8))
ax2.set_ylim((0,2))
(0, 2)
fig, (ax1, ax2) = plt.subplots(1, 2,figsize=(18,7))
df_curry_pull_up=df_shots.query('player_id==201939 and Shot_type=="pull-up 3P"').copy()
df_curry_catch=df_shots.query('player_id==201939 and Shot_type=="catch-and-shoot 3P"').copy()
for row in df_curry_pull_up.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax1.plot(Time,D,'b-',alpha=0.1)
#ax2.plot(Time,T,'b-',alpha=0.1)
k+=1
for row in df_curry_catch.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax2.plot(Time,D,'r-',alpha=0.1)
#ax2.plot(Time,T,'r-',alpha=0.1)
sns.lineplot(x="Time", y='D', data=df_curry_mean[df_curry_mean['Shot_type']=="pull-up 3P"],hue='Shot_type',palette=['black'],ax=ax1,size_order=[60])
ax1.set_xlim((-3.2,0.8))
ax1.set_ylim((0,30))
sns.lineplot(x="Time", y='D', data=df_curry_mean[df_curry_mean['Shot_type']=="catch-and-shoot 3P"],hue='Shot_type',palette=['black'],ax=ax2,size_order=[60])
ax2.set_xlim((-3.2,0.8))
ax2.set_ylim((0,30))
(0, 30)
fig, (ax1, ax2) = plt.subplots(1, 2,figsize=(20,7))
#for row in df_curry_pull_up.iloc[:3].itertuples():
# Time=getattr(row,'Time')
# D=getattr(row,'D')
# T=getattr(row,'T')
# tts=getattr(row,'Time_to_shoot')[0]
# ax1.axvline(tts, color='b',linestyle="dashed",lw=0.5)
# ax2.axvline(tts, color='b',linestyle="dashed",lw=0.5)
# ax1.plot(Time,D,'b-',alpha=0.8)
# ax2.plot(Time,T,'b-',alpha=0.8)
for row in df_curry_catch.iloc[100:101].itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
tts=getattr(row,'release_time')
print(getattr(row,'Match_id'),getattr(row,'quarter'),getattr(row,'clock'),getattr(row,'player_id'))
ax1.axvline(tts, color='black',linestyle="dashed",lw=0.5)
ax2.axvline(tts, color='black',linestyle="dashed",lw=0.5)
ax1.plot(Time,D,'r-',alpha=0.8)
ax2.plot(Time,T,'r-',alpha=0.8)
ax1.set_xlim((-3.2,0.8))
ax1.set_ylim((0,30))
ax2.set_xlim((-3.2,0.8))
ax2.set_ylim((0,2))
397 1 295.28 201939
(0, 2)
df_other_mean=df_plot_mean.query('player_id==202691').copy()
averaged_evolution_two_df(df_other_mean,df_curry_mean,sd=None,hue='Shot_type',palette=['blue','orange'],style='player_id')
<Figure size 432x288 with 0 Axes>
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2, 2,figsize=(20,14))
df_other_mean=df.query('player_id==202323').copy()
df_curry_pull_up=df_shots.query('player_id==201939 and Shot_type=="pull-up 3P"').copy()
df_other_pull_up=df_shots.query('player_id==202323 and Shot_type=="pull-up 3P"').copy()
df_curry_catch=df_shots.query('player_id==201939 and Shot_type=="catch-and-shoot 3P"').copy()
df_other_catch=df_shots.query('player_id==202323 and Shot_type=="catch-and-shoot 3P"').copy()
print('number of shots Curry:',len(df_curry_pull_up)+len(df_curry_catch))
print('percentage of CandS Curry:',len(df_curry_catch)/(len(df_curry_catch)+len(df_curry_pull_up)))
print('number of shots Other:',len(df_other_pull_up)+len(df_other_catch))
print('percentage of CandS Other:',len(df_other_catch)/(len(df_other_catch)+len(df_other_pull_up)))
for row in df_curry_pull_up.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax1.plot(Time,D,'b-',alpha=0.1)
#ax3.plot(Time,T,'b-',alpha=0.1)
for row in df_curry_catch.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax3.plot(Time,D,'b-',alpha=0.1)
#ax3.plot(Time,T,'b-',alpha=0.1)
for row in df_other_pull_up.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax2.plot(Time,D,'r-',alpha=0.1)
#ax2.plot(Time,T,'r-',alpha=0.1)
for row in df_other_catch.itertuples():
Time=getattr(row,'Time')
D=getattr(row,'D')
T=getattr(row,'T')
ax4.plot(Time,D,'r-',alpha=0.1)
ax1.set_xlim((-3.2,0.8))
ax1.set_ylim((0,30))
sns.lineplot(x="Time", y='D', data=df_curry_mean[df_curry_mean['Shot type']=="pull-up 3P"],hue='Shot type',palette=['black'],ax=ax1,size_order=[60])
ax2.set_xlim((-3.2,0.8))
ax2.set_ylim((0,30))
sns.lineplot(x="Time", y='D', data=df_other_mean[df_other_mean['Shot type']=="pull-up 3P"],hue='Shot type',palette=['black'],ax=ax2,size_order=[60])
ax3.set_xlim((-3.2,0.8))
ax3.set_ylim((0,30))
sns.lineplot(x="Time", y='D', data=df_curry_mean[df_curry_mean['Shot type']=="catch-and-shoot 3P"],hue='Shot type',palette=['black'],ax=ax3,size_order=[60])
ax4.set_xlim((-3.2,0.8))
ax4.set_ylim((0,30))
sns.lineplot(x="Time", y='D', data=df_other_mean[df_other_mean['Shot type']=="catch-and-shoot 3P"],hue='Shot type',palette=['black'],ax=ax4,size_order=[60])
number of shots Curry: 390 percentage of CandS Curry: 0.5153846153846153 number of shots Other: 56 percentage of CandS Other: 0.9642857142857143
<matplotlib.axes._subplots.AxesSubplot at 0x11c83ca90>
df_other_mean=df_plot_mean.query('player_id==203490').copy()
def averaged_evolution_two_players(df_player1,df_player2,sd=None,hue=None,palette=['blue'],style=None,size=None):
## rename columns to have good labels ##
df=pd.concat([df_player1,df_player2],ignore_index=True)
df=df.rename(columns={"Time": "time [s]",'D':r'$<\delta_{d}^*(t)$> [feet]','T':r'$<\delta_{t}^*(t)$> [s]'})
### plot ###
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{d}^*(t)$> [feet]', data=df,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize='small')
plt.show()
plt.clf()
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{t}^*(t)$> [s]', data=df,palette=palette,ci=sd,hue=hue,style=style,size=size)
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize='small')
#plt.savefig('Comparison_curry_t',dpi=72)
plt.show()
plt.clf()
averaged_evolution_two_df(df_other_mean,df_curry_mean,sd=None,hue='Shot_type',palette=['blue','orange'],style='player_id')
<Figure size 432x288 with 0 Axes>
def d_reception(row):
"Calculate distance of the closest player at ball reception"
D=row['D']
Time=row['Time']
TTS=row['release_time']
if TTS<-3:
return(D[0])
i=0
while i<len(Time) and round(Time[i],2)!=round(TTS,2):
i+=1
if i>=len(D):
return D[0]
return D[i]
def t_reception(row):
"Calculate distance of the closest player at ball reception"
T=row['T']
Time=row['Time']
TTS=row['release_time']
if TTS<-3:
return(T[0])
i=0
while i<len(Time) and round(Time[i],2)!=round(TTS,2):
i+=1
if i>=len(T):
return T[0]
return T[i]
def d_release(row):
"Calculate distance of the closest player at ball release"
D=row['D']
Time=row['Time']
ind=Time.index(0.)
return D[ind]
def t_release(row):
"Calculate distance of the closest player at ball release"
T=row['T']
Time=row['Time']
ind=Time.index(0.)
return T[ind]
df_shots['d_release']=df_shots.apply(d_release,axis=1)
df_shots['t_release']=df_shots.apply(t_release,axis=1)
df_shots['d_reception']=df_shots.apply(d_reception,axis=1)
df_shots['t_reception']=df_shots.apply(t_reception,axis=1)
from scipy import stats
g=sns.jointplot(x='d_reception',y='release_time', data=df_shots.query('Shot_type=="catch-and-shoot 3P" and d_reception<30'),kind='reg')
g.annotate(stats.pearsonr)
/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py:1847: UserWarning: JointGrid annotation is deprecated and will be removed in a future release. warnings.warn(UserWarning(msg))
<seaborn.axisgrid.JointGrid at 0x1a446be2d0>
g = sns.JointGrid(x='d_reception',y='release_time', data=df_shots.query('player_id==203914 and Shot_type=="catch-and-shoot 3P"'))
g.plot_joint(sns.regplot,color='blue')
g.plot_marginals(sns.distplot,color='blue')
#g.annotate(stats.pearsonr)
g.x=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"')['d_reception']
g.y=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"')['release_time']
g.plot_joint(sns.regplot,color='red')
g.plot_marginals(sns.distplot,color='red')
#g.annotate(stats.pearsonr)
g.ax_joint.set_xlim((0,25))
#df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"').plot(x='d_reception',y='Time_to_shoot',alpha=0.7,kind='scatter',ax=ax)
#df_shots.query('player_id==202331 and Shot_type=="catch-and-shoot 3P"').plot(x='d_reception',y='Time_to_shoot',alpha=0.7,kind='scatter',ax=ax,color='red')
(0, 25)
from scipy import stats
g = sns.JointGrid(x='t_reception',y='release_time', data=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P" and Shot_result==0'))
g.plot_joint(sns.regplot,color='blue')
g.plot_marginals(sns.distplot,color='blue')
g.x=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P" and Shot_result==1')['t_reception']
g.y=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P" and Shot_result==1')['release_time']
g.plot_joint(sns.regplot,color='red')
g.plot_marginals(sns.distplot,color='red')
g.annotate(stats.pearsonr)
#g.ax_joint.set_xlim((0,25))
<seaborn.axisgrid.JointGrid at 0x1a5e2f9e90>
fig,ax=plt.subplots(1,1,figsize=(10,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
#df_plot_shots=df_shots_mean.join(df_stats.query('total_cas>100'))
#df_plot_shots['player_id']=df_shots_mean.index
#df_shots_mean.plot(x='d_reception',y='Time_to_shoot',kind='scatter',ax=ax)
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P"')['d_reception'],y='d_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P"'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,0.5))
ax.axis('equal')
ax.set_xlim((0,40))
ax.set_ylim((0,40))
#for line in range(df_plot_shots.shape[0]):
# p.text(-df_plot_shots.iloc[line]['release_time'], df_plot_shots.iloc[line]['d_release'], df_plot_shots.iloc[line]['player_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
(0, 40)
g = sns.regplot(x='d_reception',y='d_release', data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'))
#g.plot_joint(sns.regplot)
import scipy
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x=g.get_lines()[0].get_xdata(),y=g.get_lines()[0].get_ydata())
from scipy import stats
#fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
#df_plot_shots=df_shots_mean.join(df_stats.query('total_cas>100'))
#df_plot_shots['player_id']=df_shots_mean.index
#df_shots_mean.plot(x='d_reception',y='Time_to_shoot',kind='scatter',ax=ax)
#p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939')['d_reception'],y='d_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,0.5))
g = sns.JointGrid(x='d_reception',y='d_release', data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),height=10)
g.plot_joint(sns.regplot,ci=None)
#p=sns.regplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939')['d_reception'],y='d_release', data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),ax=ax)
g.annotate(stats.pearsonr)
g.x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==203914')['d_reception']
g.y=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==203914')['d_release']
g.plot_joint(sns.regplot,ci=None)
g.annotate(stats.pearsonr)
g.x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201143')['d_reception']
g.y=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201143')['d_release']
g.plot_joint(sns.regplot,ci=None)
#p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==1718')['d_reception'],y='d_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==1718'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,0.5))
#for line in range(df_plot_shots.shape[0]):
# p.text(-df_plot_shots.iloc[line]['release_time'], df_plot_shots.iloc[line]['d_release'], df_plot_shots.iloc[line]['player_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py:1847: UserWarning: JointGrid annotation is deprecated and will be removed in a future release. warnings.warn(UserWarning(msg))
<seaborn.axisgrid.JointGrid at 0x1ae1601b50>
from scipy import stats
g = sns.JointGrid(x='t_reception',y='release_time', data=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"'))
g.plot_joint(sns.regplot,color='blue')
g.plot_marginals(sns.distplot,color='blue')
g.annotate(stats.pearsonr)
<seaborn.axisgrid.JointGrid at 0x191b790a90>
g = sns.JointGrid(x='d_release',y='release_time', data=df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"'))
g.plot_joint(sns.regplot,color='blue')
g.plot_marginals(sns.distplot,color='blue')
g.annotate(stats.pearsonr)
<seaborn.axisgrid.JointGrid at 0x1a5db4c590>
fig,ax=plt.subplots(1,1,figsize=(10,10))
p=sns.kdeplot(df_shots.query('player_id==203914 and Shot_type=="catch-and-shoot 3P"')['d_release'],df_shots.query('player_id==203914 and Shot_type=="catch-and-shoot 3P"')['release_time'],cmap="Blues")
p=sns.kdeplot(df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"')['d_release'],df_shots.query('player_id==202691 and Shot_type=="catch-and-shoot 3P"')['release_time'],cmap="Reds")
p=sns.kdeplot(df_shots.query('player_id==200794 and Shot_type=="catch-and-shoot 3P"')['d_release'],df_shots.query('player_id==200794 and Shot_type=="catch-and-shoot 3P"')['release_time'],cmap="Greens")
fig,ax=plt.subplots(1,1,figsize=(10,10))
p=sns.scatterplot(x='d_release',y='release_time', data=df_shots.query('Shot_type=="catch-and-shoot 3P"'))
df_shots.query('Shot_type=="catch-and-shoot 3P"')['d_release'].mean()
7.045989175313013
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
df_plot_shots=df_shots_mean.join(df_stats.query('total_cas>100'))
df_plot_shots['player_id']=df_shots_mean.index
#df_shots_mean.plot(x='d_reception',y='Time_to_shoot',kind='scatter',ax=ax)
p=sns.scatterplot(x='d_reception',y='d_release',palette="Set2", data=df_plot_shots,hue='player_id',ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.,-0.5))
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939')['d_reception'],y='d_release',palette="RdBu", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),ax=ax,hue='release_time')
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['d_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['d_release'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939')['t_reception'],y='t_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,-0.5))
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['t_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['t_release'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release']].groupby('player_id').mean()
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939')['t_reception'],y='release_time',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,-0.5))
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['t_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['release_time'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==201939').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total_cas>100').index
df_shots_mean=df_shots.query('player_id in @players and Shot_type=="catch-and-shoot 3P"')[['player_id','release_time','d_reception','d_release','t_reception']].groupby('player_id').mean()
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691')['d_reception'],y='d_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691'),ax=ax,size='release_time',sizes=(20, 500),size_norm=(-1.5,-0.5))
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['d_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['d_release'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
fig,ax=plt.subplots(1,1,figsize=(15,10))
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691')['t_reception'],y='t_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691'),ax=ax,size='release_time',sizes=(20, 200),size_norm=(-1.5,-0.5))
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['t_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['t_release'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
fig,ax=plt.subplots(1,1,figsize=(10,10))
p=sns.scatterplot(x=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691')['t_reception'],y='d_release',palette="Set2", data=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691'),ax=ax,size='release_time',sizes=(20, 200),size_norm=(-1.5,-0.5))
for line in range(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').shape[0]):
p.text(df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['t_reception'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['d_release'], df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==202691').iloc[line]['shot_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
g = sns.jointplot(x='d_reception',y='d_release', data=df_shots.query('Shot_type=="catch-and-shoot 3P"'),kind='hex',height=10,joint_kws=dict(gridsize=100))
g.ax_joint.set_xlim((0,25))
g.ax_joint.set_ylim((0,25))
g.ax_joint.plot([0, 1], [0, 1], transform=ax.transAxes)
[<matplotlib.lines.Line2D at 0x1af5539b90>]
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,203914,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.set_xlim((0,25))
ax.set_ylim((0,25))
ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['d_reception'],df['d_release'],ax=ax,color='blue')
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,203914,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.set_xlim((0,25))
ax.set_ylim((0,25))
ax.axhline(7,color='k',linestyle='--')
ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(df['d_reception'],df['d_release'],ax=ax,hue=-df['release_time'],hue_norm=(0.3,1))
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,203914,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(0.5,color='k',linestyle='--')
ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(df['t_reception'],df['t_release'],ax=ax)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(0.77,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(df['t_reception'],-df['release_time'],ax=ax,alpha=0.8)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.73,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(-df['release_time'],df['d_release'],ax=ax,hue=df['d_reception'],hue_norm=(0,16),alpha=0.8)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
#ax.set_xlim((0,1.5))
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(-df['release_time'],df['d_release'],ax=ax,hue=df['t_reception'],hue_norm=(0.3,1),alpha=0.8)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,200755,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.73,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.kdeplot(-df['release_time'],df['d_release'],ax=ax,n_levels=15)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
#ax.axhline(7,color='k',linestyle='--')
#ax.axvline(0.73,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(-df['release_time'],df['d_release'],cmap='Blues',gridsize=10,bins=10)
ax.scatter(-df['release_time'],df['t_release'],alpha=0.9)
#kd=sns.kdeplot(-df['release_time'],df['d_release'],ax=ax,n_levels=15)
Exception ignored in: <function TransformNode.set_children.<locals>.<lambda> at 0x137a21830> Traceback (most recent call last): File "/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/matplotlib/transforms.py", line 178, in <lambda> ref = weakref.ref(self, lambda ref, sid=id(self), KeyboardInterrupt
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.axhline(0.5,color='k',linestyle='--')
ax.axvline(0.73,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.scatterplot(-df['release_time'],df['t_release'],ax=ax,alpha=0.8)
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.set_xlim((0,2))
ax.set_ylim((0,20))
ax.axhline(7,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['t_reception'],df['d_release'],ax=ax,color='blue')
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.set_xlim((0,1.5))
#ax.set_ylim((0,20))
#ax.axhline(7,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['t_reception'],-df['release_time'],ax=ax,color='blue')
fig,axs=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,12))
players=[201939,202691,201587,200794,2594,977,202695,2747,203918]
axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k]
ax.set_xlim((0,20))
#ax.set_ylim((0,20))
#ax.axhline(7,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['d_reception'],-df['release_time'],ax=ax,color='blue')
fig,ax=plt.subplots(1,1,figsize=(10,10))
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and btpl==True')[['d_release','d_reception','t_reception','release_time']]
#hb=ax.hexbin(-df['release_time'],df['d_release'],cmap='Blues',gridsize=20)
ax.scatter(-df['release_time'],df['d_release'],cmap='Blues',alpha=0.05)
hb=sns.kdeplot(-df['release_time'],df['d_release'])
fig,axs=plt.subplots(4,4,sharex=True,sharey=True,figsize=(10,10))
#players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
#axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k//4][k%4]
ax.set_xlim((0,1.5))
#ax.set_ylim((0,20))
ax.axhline(0.68,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
ax.set_xticks([0,0.2,0.4,0.6,0.8,1,1.2,1.4])
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['t_reception'],-df['release_time'],ax=ax,color='blue',scatter_kws={'alpha':0.5})
if k%4==0:
if k//4!=3:
ax.xaxis.set_visible(False)
ax.set_ylabel('release time [s]',fontsize='large')
else:
ax.set_xlabel(r'$\delta_{time}^*(t_{catch})$ [s]',fontsize='large')
ax.set_ylabel('release time [s]',fontsize='large')
else :
if k//4!=3:
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
else :
ax.yaxis.set_visible(False)
ax.set_xlabel(r'$\delta_{time}^*(t_{catch})$ [s]',fontsize='large')
ax.set_title(info_players.loc[p]['lastName']+' '+str(df_stats.loc[p]['percentage_cas'])+'%')
plt.tight_layout()
plt.savefig('images/t_recep_release.jpg',dpi=150)
fig,axs=plt.subplots(4,4,sharex=True,sharey=True,figsize=(10,10))
#players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
#axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k//4][k%4]
ax.set_xlim((0,1.5))
#ax.set_ylim((0,20))
ax.axhline(0.68,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
ax.set_xticks([0,0.2,0.4,0.6,0.8,1,1.2,1.4])
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['t_reception'],-df['release_time'],ax=ax,color='blue',scatter_kws={'alpha':0.5})
if k%4==0:
if k//4!=3:
ax.xaxis.set_visible(False)
ax.set_ylabel('release time [s]',fontsize='large')
else:
ax.set_xlabel(r'$\delta_{time}^*(t_{catch})$ [s]',fontsize='large')
ax.set_ylabel('release time [s]',fontsize='large')
else :
if k//4!=3:
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
else :
ax.yaxis.set_visible(False)
ax.set_xlabel(r'$\delta_{time}^*(t_{catch})$ [s]',fontsize='large')
ax.set_title(info_players.loc[p]['lastName']+' '+str(df_stats.loc[p]['percentage_cas'])+'%')
plt.tight_layout()
plt.savefig('images/t_recep_release.jpg',dpi=150)
fig,axs=plt.subplots(4,4,sharex=True,sharey=True,figsize=(12,12))
#players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
#axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k//4][k%4]
ax.set_xlim((0,1.5))
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
#ax.set_ylim((0,20))
#ax.axhline(7,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#hb=ax.hexbin(df['d_reception'],df['d_release'],cmap='Blues',gridsize=50)
#ax.scatter(df['d_reception'],df['d_release'],alpha=0.9)
kd=sns.regplot(df['t_reception'],df['d_release'],ax=ax,color='blue')
fig,axs=plt.subplots(4,4,sharex=True,sharey=True,figsize=(10,10))
#players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
#axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k//4][k%4]
ax.set_xlim((0,1.5))
ax.set_ylim((0,16))
ax.axhline(2,color='k',linestyle='--',linewidth=0.4)
ax.axhline(4,color='k',linestyle='--',linewidth=0.4)
ax.axhline(6,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
ax.set_yticks([0,2,4,6,8,10,12,14,16,18,20])
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
#kd=sns.kdeplot(-df['release_time'],df['d_release'],ax=ax,n_levels=12)
sc=sns.scatterplot(-df['release_time'],df['d_release'],ax=ax,alpha=0.5,hue=df['Shot_result'],palette=['red','blue'],legend=False)
#kd=sns.regplot(-df['release_time'],df['d_release'],ax=ax,color='blue')
#hb=ax.hexbin(-df['release_time'],df['d_release'],cmap='Blues',gridsize=10)
if k%4==0:
if k//4!=3:
ax.xaxis.set_visible(False)
ax.set_ylabel(r'$\delta_{space}^*(t=t_{shot})$ [feet]',fontsize='large')
else:
ax.set_xlabel('release time [s]',fontsize='large')
ax.set_ylabel(r'$\delta_{space}^*(t=t_{shot})$ [feet]',fontsize='large')
else :
if k//4!=3:
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
else :
ax.yaxis.set_visible(False)
ax.set_ylim((0,16))
ax.set_xlabel('release time [s]',fontsize='large')
ax.set_title(info_players.loc[p]['lastName']+' '+str(df_stats.loc[p]['percentage_cas'])+'%')
plt.tight_layout()
fig,axs=plt.subplots(4,4,sharex=True,sharey=True,figsize=(12,12))
#players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
#axs=[axs[0][0],axs[0][1],axs[0][2],axs[1][0],axs[1][1],axs[1][2],axs[2][0],axs[2][1],axs[2][2]]
for k in range(len(players)):
p=players[k]
ax=axs[k//4][k%4]
ax.set_xlim((0,1.5))
ax.set_ylim((0,20))
ax.axhline(7,color='k',linestyle='--')
ax.axvline(0.68,color='k',linestyle='--')
#ax.plot([0, 1], [0, 1], transform=ax.transAxes,color='k',linestyle='--')
df=df_shots.query('Shot_type=="catch-and-shoot 3P" and player_id==@p and btpl==True')
kd=sns.scatterplot(-df['release_time'],df['d_release'],ax=ax,alpha=0.5)
#kd=sns.regplot(-df['release_time'],df['d_release'],ax=ax,color='blue')
players=[203490,2594,977,201569,202325,101123,1626156,101141,1717,202695,200755,202691,201939,200768,201142,202083]
players=df_stats.loc[players].sort_values('percentage_cas',ascending=False).index.tolist()
df_stats.query('total_cas>100').sort_values(by='percentage_cas',ascending=True).head()
total | success | miss | percentage | match_played | total_cas | success_cas | miss_cas | percentage_cas | |
---|---|---|---|---|---|---|---|---|---|
203490 | 121 | 33 | 88 | 27.3 | 34 | 112 | 31 | 81 | 27.7 |
201143 | 113 | 31 | 82 | 27.4 | 41 | 109 | 31 | 78 | 28.4 |
1718 | 123 | 34 | 89 | 27.6 | 35 | 111 | 32 | 79 | 28.8 |
977 | 220 | 54 | 166 | 24.5 | 36 | 128 | 37 | 91 | 28.9 |
202329 | 160 | 51 | 109 | 31.9 | 44 | 139 | 43 | 96 | 30.9 |
201158 | 156 | 44 | 112 | 28.2 | 38 | 122 | 38 | 84 | 31.1 |
202325 | 123 | 42 | 81 | 34.1 | 36 | 107 | 34 | 73 | 31.8 |
202335 | 135 | 44 | 91 | 32.6 | 40 | 130 | 42 | 88 | 32.3 |
200794 | 113 | 38 | 75 | 33.6 | 40 | 104 | 34 | 70 | 32.7 |
1626156 | 176 | 53 | 123 | 30.1 | 40 | 109 | 36 | 73 | 33.0 |
201980 | 152 | 47 | 105 | 30.9 | 41 | 127 | 42 | 85 | 33.1 |
202711 | 149 | 47 | 102 | 31.5 | 40 | 123 | 41 | 82 | 33.3 |
204001 | 114 | 39 | 75 | 34.2 | 41 | 109 | 37 | 72 | 33.9 |
201567 | 180 | 59 | 121 | 32.8 | 38 | 159 | 54 | 105 | 34.0 |
101107 | 173 | 60 | 113 | 34.7 | 42 | 170 | 58 | 112 | 34.1 |
print(df_shots.query('Shot_type=="catch-and-shoot 3P"')['release_time'].median())
print(df_shots.query('Shot_type=="catch-and-shoot 3P"')['t_reception'].median())
df_shots.query('Shot_type=="catch-and-shoot 3P"')['d_release'].median()
-0.6800000000000114 0.6773187746076399
6.3601167897351925
def unit_vector(vector):
""" Returns the unit vector of the vector. """
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
""" Returns the angle in radians between vectors 'v1' and 'v2'::
>>> angle_between((1, 0, 0), (0, 1, 0))
1.5707963267948966
>>> angle_between((1, 0, 0), (1, 0, 0))
0.0
>>> angle_between((1, 0, 0), (-1, 0, 0))
3.141592653589793
"""
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.rad2deg(np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0)))
def opponent_angle(row):
opp_pos=row['opp_position_release']
player_pos=row['player_position_release']
if player_pos[0]>94//2:
basket_pos=[94-5.25,25]
else :
basket_pos=[5.25,25]
v1=[basket_pos[0]-player_pos[0],basket_pos[1]-player_pos[1]]
v2=[opp_pos[0]-player_pos[0],opp_pos[1]-player_pos[1]]
return(angle_between(v1,v2))
df_shots['opp_angle']=df_shots.apply(opponent_angle,axis=1)
/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in true_divide This is separate from the ipykernel package so we can avoid doing imports until
sns.kdeplot(df_shots.query('player_id==202691')['opp_angle'])
<matplotlib.axes._subplots.AxesSubplot at 0x1b5036aa10>
def opp_position_release(row):
Time=row['Time']
ind=Time.index(0.)
return(row['opp_pos'][ind])
def player_position_release(row):
Time=row['Time']
ind=Time.index(0.)
return(row['shooter_pos'][ind])
df_shots['opp_position_release']=df_shots.apply(opp_position_release,axis=1)
df_shots['player_position_release']=df_shots.apply(player_position_release,axis=1)
def distance(a,b): #a = (x,y) point de départ ; b = (i,j) point d'arrivée ; v = norme pour l'instant
return np.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)
def behind_three_point_line(row):
p=row['player_position_release']
x_ball=row['x_ball']
if x_ball[-1]>94/2:
where=0
else :
where=1
coin=False
if where==1 :
basket_pos=[5.25,25]
if p[0]<15:
coin=True
else :
basket_pos=[94-5.25,25]
if p[0]>(94-15):
coin=True
if coin :
if 0<p[1]<3.5 or 50-3.5<p[1]<50 :
return (True)
else :
return (False)
else :
if distance(p,basket_pos)>23.5 : #In fact 23.75 but we take a marge to have all shoots
return (True)
else:
return (False)
df_shots['btpl']=df_shots.apply(behind_three_point_line,axis=1)
print(len(df_shots),len(df_shots.query('btpl==True')))
m1=df_shots.query('btpl==True and Shot_type=="catch-and-shoot 3P"').groupby('Shot_result').count().loc[0,'D']
s1=df_shots.query('btpl==True and Shot_type=="catch-and-shoot 3P"').groupby('Shot_result').count().loc[1,'D']
m2=df_shots.groupby('Shot_result').count().loc[0,'D']
s2=df_shots.groupby('Shot_result').count().loc[1,'D']
print(s1/(s1+m1),s2/(s2+m2))
print(len(df_shots.query('Shot_type=="catch-and-shoot 3P"'))/len(df_shots)*100)
print(len(df_shots.query('Shot_type=="catch-and-shoot 3P" and btpl==True'))/len(df_shots.query("btpl==True"))*100)
28050 26297 0.3596867227602265 0.3536541889483066 69.18360071301248 71.85990797429365
fig,ax=plt.subplots(1,1,figsize=(6,6))
df=df_shots.query('player_id==201939 and btpl==True')
for i in range(len(df)):
x1=df.iloc[i]['player_position_release'][0]
x2=df.iloc[i]['opp_position_release'][0]
y1=50-df.iloc[i]['player_position_release'][1]
y2=50-df.iloc[i]['opp_position_release'][1]
if x1>94/2:
x1=94-x1
x2=94-x2
y1=50-y1
y2=50-y2
if i!=len(df)-1:
plt.plot([x1,x2],[y1,y2],'k',alpha=0.6)
if df.iloc[i]['Shot_result']==1:
plt.plot(x1,y1,'bo',alpha=0.6)
else:
plt.plot(x1,y1,'ro',alpha=0.6)
else :
plt.plot([x1,x2],[y1,y2],'k',alpha=0.6,label='distance closest defender')
if df.iloc[i]['Shot_result']==1:
plt.plot(x1,y1,'bo',alpha=0.6,label='success')
else:
plt.plot(x1,y1,'ro',alpha=0.6,label='miss')
#plt.legend()
field = plt.imread("Images/fullcourt1.png")
plt.imshow(field, extent=[0,50,0,50])
plt.axis('off')
(0.0, 50.0, 0.0, 50.0)
df=df_shots.query('player_id==201939')
for i in range(len(df)):
x1=df.iloc[i]['player_position_release'][0]
x2=df.iloc[i]['opp_position_release'][0]
y1=df.iloc[i]['player_position_release'][1]
y2=df.iloc[i]['opp_position_release'][1]
plt.plot([x1,x2],[y1,y2])
plt.plot(x1,y1,'o')
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
def distance_to_basket(row):
z_ball=row['z_ball']
x_ball=row['x_ball']
y_ball=row['y_ball']
if x_ball[-1]>94//2:
basket_pos=[94-5.25,25]
else :
basket_pos=[5.25,25]
d_basket=[]
for k in range(len(x_ball)):
d_basket.append(-np.sqrt((x_ball[k]-basket_pos[0])**2+(y_ball[k]-basket_pos[1])**2))
return(d_basket)
def shoot_traj(df_traj,i):
z_ball=df_traj.query('shot_id==@i')['z_ball'].iloc[0]
d_basket=df_traj.query('shot_id==@i')['d_basket'].iloc[0]
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(15,5))
ax1.plot(df_traj.query('shot_id==@i')['Time'].iloc[0],z_ball)
ax2.plot(df_traj.query('shot_id==@i')['d_basket'].iloc[0],z_ball)
#plt.axvline(-22, color='black',linestyle="dashed",lw=0.5)
#plt.axvline(-23.75, color='black',linestyle="dashed",lw=0.5)
#ax1.set_ylim((0,100))
plt.show()
df_shots['d_basket']=df_shots.apply(distance_to_basket,axis=1)
shoot_traj(df_shots,401)
def z_angle(row):
z_ball=row['z_ball']
d_basket=row['d_basket']
angle=[]
for k in range(len(d_basket)-1):
z1=z_ball[k]
z2=z_ball[k+1]
d1=d_basket[k]
d2=d_basket[k+1]
if (d2-d1)<=0:
angle.append(np.degrees(np.arctan((z2-z1)/(d2-d1)))+180)
else :
if (z2-z1)<=0:
angle.append(np.degrees(np.arctan((z2-z1)/(d2-d1)))+360)
else:
angle.append(np.degrees(np.arctan((z2-z1)/(d2-d1))))
return(angle)
df_shots['angle']=df_shots.apply(z_angle,axis=1)
/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:11: RuntimeWarning: invalid value encountered in double_scalars # This is added back by InteractiveShellApp.init_path()
def traj(x, velocity, angle, h, g=9.8):
angle = np.deg2rad(angle)
lhs = x * np.tan(angle)
rhs_top = g * x ** 2
rhs_bottom = (2*(velocity)**2) * (np.cos(angle)**2)
return h+lhs - (rhs_top / rhs_bottom)
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
def test(df_traj):
n=0
s=11
print(len(df_traj.query('precise_angle<30')))
Time=df_traj.query('precise_angle<30').iloc[s]['Time']
time_end=-1
z_ball=df_traj.query('precise_angle<30').iloc[s]['z_ball']
angle=df_traj.query('precise_angle<30').iloc[s]['angle']
time_start=z_ball.index(max(z_ball))
time_start-=1
print(time_start,z_ball[time_start])
while z_ball[time_start]>8.2 and (30<angle[time_start] or angle[time_start]<70):
print(time_start,z_ball[time_start])
time_start-=1
time_start+=1
time_end=z_ball.index(max(z_ball))
time_end+=1
while time_end<len(Time) and z_ball[time_end]>12:
time_end+=1
time_end-=1
#while time_start<len(Time) and Time[time_start]<-1.:
# time_start+=1
#while z_ball[time_start]<8.2 or (30>angle[time_start] or angle[time_start]>70):
# time_start+=1
print(time_start)
#time_start-=6
#time_start=0
z1=df_traj.query('precise_angle<30').iloc[s]['z_ball'][time_start:time_end]
x1=np.array(df_traj.query('precise_angle<30').iloc[s]['d_basket'][time_start:time_end])
x1=x1-x1[0]
#print(Time[time_start])
plt.plot(x1, z1, 'b-', label='data')
popt, pcov = curve_fit(traj, x1, z1, p0=[19,45,8])
plt.plot(x1, traj(x1, *popt), 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
#print(df_shots.query('precise_angle_bis>70').iloc[s]['angle'][time_start])
print(popt[1]%90)
print(angle[time_start])
print(len(x1))
#print(df_traj.query('precise_angle>70').iloc[s][['shot_id','Match_id','quarter','clock']])
test(df_shots)
406 90 13.17209 90 13.17209 89 12.93329 88 12.92917 87 12.70606 86 12.48232 85 12.29262 84 12.02587 83 11.70262 82 11.32003 81 10.8752 80 10.36527 79 9.78738 78 9.33121 77 8.75718 77 23.848716828745687 21.97605403533036 20
sns.kdeplot(df_shots['precise_angle'])
sns.kdeplot(df_shots.query('player_id==201939')['precise_angle'])
<matplotlib.axes._subplots.AxesSubplot at 0x15ed60350>
def release_d_basket(row):
x=row['x_shooter']
y=row['y_shooter']
x_ball=row['x_ball']
if x_ball[-1]>94//2:
basket_pos=[94-5.25,25]
else :
basket_pos=[5.25,25]
return(np.sqrt((x-basket_pos[0])**2+(y-basket_pos[1])**2))
df_shots['release_d_basket']=df_shots.apply(release_d_basket,axis=1)
g=sns.jointplot(x='release_d_basket',y='precise_angle', data=df_shots.query('25<precise_angle<65 and release_d_basket<27.5'),kind='reg')
g=sns.jointplot(x='release_d_basket',y='precise_angle', data=df_shots[df_shots['Shot result']==1].query('25<precise_angle<65 and release_d_basket<27.5'),kind='reg')
g.x=df_shots[df_shots['Shot result']==0].query('25<precise_angle<65 and release_d_basket<27.5')['release_d_basket']
g.y=df_shots[df_shots['Shot result']==0].query('25<precise_angle<65 and release_d_basket<27.5')['precise_angle']
g.plot_joint(sns.scatterplot,color='red',alpha=0.4)
g.plot_marginals(sns.distplot,color='red')
<seaborn.axisgrid.JointGrid at 0x15b8e3f10>
sns.kdeplot(df_shots.query('Shot_type=="catch-and-shoot 3P"')['new_Time_to_shoot'])
<matplotlib.axes._subplots.AxesSubplot at 0x1a21e67350>
print(len(df_shots.query('Match_id=="66"')))
30
g=sns.jointplot(x='d_closest_release',y='precise_angle', data=df_shots,kind='reg')
from scipy import stats
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total>30').index
df_shots_mean=df_shots.query('player_id in @players')[['player_id','precise_angle']].groupby('player_id').mean()
print(df_shots.query('player_id in @players')[['player_id','precise_angle']].mean())
df_plot_shots=df_shots_mean.join(df_stats.query('total>30'))
df_plot_shots['player_id']=df_shots_mean.index
#df_shots_mean.plot(x='d_reception',y='Time_to_shoot',kind='scatter',ax=ax)
p=sns.scatterplot(x='precise_angle',y='percentage',palette="Set2", data=df_plot_shots,hue='player_id',ax=ax)
for line in range(df_plot_shots.shape[0]):
p.text(df_plot_shots.iloc[line]['precise_angle'], df_plot_shots.iloc[line]['percentage'], df_plot_shots.iloc[line]['player_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
slope, intercept, r_value, p_value, std_err = stats.linregress(df_plot_shots['precise_angle'], df_plot_shots['percentage'])
print('R^2 : ',r_value**2)
player_id 222698.068163 precise_angle 47.374206 dtype: float64 R^2 : 8.954918032853495e-05
fig,ax=plt.subplots(1,1,figsize=(15,10))
players=df_stats.query('total>30').index
df_shots_mean=df_shots.query('player_id in @players')[['player_id','precise_angle','d_closest_release']].groupby('player_id').mean()
print(df_shots.query('player_id in @players')[['player_id','precise_angle','d_closest_release']].mean())
df_plot_shots=df_shots_mean.join(df_stats.query('total>30'))
df_plot_shots['player_id']=df_shots_mean.index
#df_shots_mean.plot(x='d_reception',y='Time_to_shoot',kind='scatter',ax=ax)
p=sns.scatterplot(x='precise_angle',y='d_closest_release',palette="Set2", data=df_plot_shots,hue='player_id',ax=ax)
for line in range(df_plot_shots.shape[0]):
p.text(df_plot_shots.iloc[line]['precise_angle'], df_plot_shots.iloc[line]['d_closest_release'], df_plot_shots.iloc[line]['player_id'], horizontalalignment='center', verticalalignment='top',size='small', color='black', weight='semibold')
slope, intercept, r_value, p_value, std_err = stats.linregress(df_plot_shots['precise_angle'], df_plot_shots['d_closest_release'])
print('R^2 : ',r_value**2)
player_id 222698.068163 precise_angle 47.374206 d_closest_release 6.630773 dtype: float64 R^2 : 0.0014610634071090013
g = sns.JointGrid(x='precise_angle',y='d_closest_release', data=df_shots.query('player_id==202695'))
g.plot_joint(sns.scatterplot,color='blue',alpha=0.6)
g.plot_marginals(sns.distplot,color='blue')
g.x=df_shots.query('player_id==201939')['precise_angle']
g.y=df_shots.query('player_id==201939')['d_closest_release']
g.plot_joint(sns.scatterplot,color='red',alpha=0.6)
g.plot_marginals(sns.distplot,color='red')
g.ax_joint.set_xlim((40,60))
(40, 60)
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import linkage
df_shots_catch=df_shots.query('Shot_type=="catch-and-shoot 3P"')
def prepare_df_kmean_T(row):
T=row['T']
Time=row['Time']
ind=Time.index(0.)
if ind-75>0:
if ind+20<len(T):
return(np.array(T[ind-75:ind+20]))
return np.nan
def prepare_df_kmean_D(row):
D=row['D']
Time=row['Time']
ind=Time.index(0.)
if ind-75>0:
if ind+20<len(D):
return(np.array(D[ind-75:ind+20]))
return np.nan
df_shots_catch['D']=df_shots_catch.apply(prepare_df_kmean_D,axis=1)
df_shots_catch['T']=df_shots_catch.apply(prepare_df_kmean_T,axis=1)
#kmeans = KMeans(n_clusters=2, random_state=0).fit(df_shots['D'].values)
/Users/gabin/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:25: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy /Users/gabin/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:26: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df=df_shots_catch.dropna()
D=df[['D']].values
T=df[['T']].values
Time=df[['Time']].values
D_bis=[]
T_bis=[]
print(len(D[0][0]))
print(len(T[0][0]))
for k in range(len(D)):
D_bis.append(D[k][0])
T_bis.append(T[k][0])
if len(D[k][0])!=95:
print(len(D[k]))
95 95
kmeans_T=KMeans(n_clusters=2).fit(T_bis)
kmeans_D=KMeans(n_clusters=2).fit(D_bis)
c_T=kmeans_T.cluster_centers_
print(kmeans_T.inertia_)
plt.plot(c_T[0],'k-')
plt.plot(c_T[1],'k--')
39409.67240068314
[<matplotlib.lines.Line2D at 0x1873f6510>]
c_D=kmeans_D.cluster_centers_
print(kmeans_D.inertia_)
plt.plot(c_D[0],'k-')
plt.plot(c_D[1],'k--')
16114179.557777911
[<matplotlib.lines.Line2D at 0x17581bad0>]
from matplotlib.cbook import get_sample_data
def kmean_ev(df,c,Time,sd=None,hue=None,palette=['blue'],style=None,fontsize='medium',size=None):
im = plt.imread(get_sample_data('/Users/gabin/Ordinateur/Documents/CENTRALE_LYON_1A/PaR/bakset-image.png'))
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
## rename columns to have good labels ##
df2=df.rename(columns={"Time": "time [s]",'D':r'$<\delta_{d}^*(t)$> [feet]','T':r'$<\delta_{t}^*(t)$> [s]'})
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{d}^*(t)$> [feet]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size,label='pull-up')
plt.plot(Time[1:-4],c[0],color='orange',linestyle='-',label='catch-and-shoot 1')
plt.plot(Time[1:-4],c[1],color='orange',linestyle='--',label='catch-and-shoot 2')
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize=fontsize)
plt.annotate('shot time',(-0.37,12.5))
newax = fig.add_axes([0.63, 0.65, 0.2, 0.2], anchor='NE', zorder=1)
newax.imshow(im)
newax.axis('off')
plt.show()
plt.clf()
im = plt.imread(get_sample_data('/Users/gabin/Ordinateur/Documents/CENTRALE_LYON_1A/PaR/bakset-image.png'))
### plot ###
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
ax = sns.lineplot(x="time [s]", y=r'$<\delta_{t}^*(t)$> [s]', data=df2,palette=palette,ci=sd,hue=hue,style=style,size=size,label='pull-up')
plt.plot(Time[1:-4],c[0],color='orange',linestyle='-',label='catch-and-shoot 1')
plt.plot(Time[1:-4],c[1],color='orange',linestyle='--',label='catch-and-shoot 2')
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.legend(loc='lower left',fontsize=fontsize)
plt.annotate('shot time',(-0.37,0.84))
newax = fig.add_axes([0.63, 0.65, 0.2, 0.2], anchor='NE', zorder=1)
newax.imshow(im)
newax.axis('off')
plt.show()
plt.clf()
4.5*4/6
3.0
kmean_ev(df_plot_mean.query('Shot_type=="pull-up 3P"'),c,Time,sd=None,hue=None,palette=['blue'],style=None,fontsize='medium',size=None)
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
kmean_ev(df_plot_mean.query('Shot_type=="pull-up 3P"'),c,Time,sd=None,hue=None,palette=['blue'],style=None,fontsize='medium',size=None)
No handles with labels found to put in legend.
<Figure size 432x288 with 0 Axes>
import random as rd
catch=[]
pull=[]
for k in range(50):
s=rd.randint(0,17000)
df=df_shots.iloc[s]
if df['Shot_type']=="catch-and-shoot 3P":
catch.append(s)
plt.plot(df['Time'],df['D'],c='orange',alpha=0.3)
else:
pull.append(s)
plt.plot(df['Time'],df['D'],c='blue',alpha=0.2)
plt.xlim((-3.2,1))
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / N
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
for c in catch[:-1]:
df=df_shots.iloc[c]
plt.plot(df['Time'],df['D'],c='orange',alpha=0.3)
for c in pull[:-1]:
df=df_shots.iloc[c]
plt.plot(df['Time'],df['D'],c='blue',alpha=0.2)
df=df_shots.iloc[catch[-1]]
plt.plot(df['Time'],df['D'],c='orange',alpha=0.3,label="catch-and-shoot")
df=df_shots.iloc[pull[-1]]
plt.plot(df['Time'],df['D'],c='blue',alpha=0.2,label="pull")
plt.xlabel("time [s]")
plt.ylabel(r'$\delta_{d}^*(t)$ [feet]')
plt.legend(loc='upper center')
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.xlim((-3.2,1))
im = plt.imread(get_sample_data('/Users/gabin/Ordinateur/Documents/CENTRALE_LYON_1A/PaR/bakset-image.png'))
plt.annotate('shot time',(-0.37,16))
newax = fig.add_axes([0.63, 0.65, 0.2, 0.2], anchor='NE', zorder=1)
newax.imshow(im)
newax.axis('off')
(-0.5, 999.5, 999.5, -0.5)
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
for c in catch[:-1]:
df=df_shots.iloc[c]
T=running_mean(df['T'],3)
plt.plot(df['Time'][:-2],T,c='orange',alpha=0.3)
for c in pull[:-1]:
df=df_shots.iloc[c]
T=running_mean(df['T'],3)
plt.plot(df['Time'][:-2],T,c='blue',alpha=0.2)
df=df_shots.iloc[catch[-1]]
plt.plot(df['Time'],df['T'],c='orange',alpha=0.3,label="catch-and-shoot")
df=df_shots.iloc[pull[-1]]
plt.plot(df['Time'],df['T'],c='blue',alpha=0.2,label="pull")
plt.legend(loc='upper center')
plt.xlabel("time [s]")
plt.ylabel(r'$\delta_{t}^*(t)$ [s]')
plt.axvline(0, color='black',linestyle="dashed",lw=0.5)
plt.xlim((-3.2,1))
im = plt.imread(get_sample_data('/Users/gabin/Ordinateur/Documents/CENTRALE_LYON_1A/PaR/bakset-image.png'))
plt.annotate('shot time',(-0.37,1.05))
newax = fig.add_axes([0.63, 0.65, 0.2, 0.2], anchor='NE', zorder=1)
newax.imshow(im)
newax.axis('off')
(-0.5, 999.5, 999.5, -0.5)
We want to quantify the following intuition : "the more free a shooter is, the more efficient he is". To do so, we evaluate 3-point efficiency function of the distance (and time) within the opponent defender is.
t_abs=[0.15]+[0.1*(i+3) for i in range(12)]
inferior_abs=[]
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
for d in t_abs:
l=[7641,14919]
m=df_shots.query("t_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[0,'D']
s=df_shots.query("t_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[1,'D']
#print(m,s,s/(s+m))
#print(m+s)
inferior_abs.append(s/(s+m)*100)
plt.vlines(t_abs[3], inferior_abs[0], inferior_abs[3], linestyle="dashed",lw=0.5)
plt.hlines(inferior_abs[3], t_abs[0], t_abs[3], linestyle="dashed",lw=0.5)
plt.axhline(34.7,linestyle="dashed",lw=0.5,color='red',label='average shooting percentage')
plt.xlim((0.15,1.25))
plt.ylim((min(inferior_abs),37))
plt.xlabel(r'$\delta_{time}^*(0)$ inferior to [s]')
plt.ylabel(r'3-point shooting percentage')
plt.legend()
plt.plot(t_abs,inferior_abs)
print(inferior_abs[4])
34.32047018087385
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
d_abs=[1*(i+1) for i in range(16)]
inferior_abs=[]
for d in d_abs:
l=[7641,14919]
#if d==2:
# print(df_shots.query("1<d_release<@d and btpl==True and Shot_result==1 and shot_id not in @l")[['shot_id']])
m=df_shots.query("d_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[0,'D']
s=df_shots.query("d_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[1,'D']
inferior_abs.append(s/(s+m)*100)
plt.vlines(d_abs[3], inferior_abs[0], inferior_abs[3], linestyle="dashed",lw=0.5)
plt.hlines(inferior_abs[3], d_abs[0], d_abs[3], linestyle="dashed",lw=0.5)
plt.vlines(d_abs[7], inferior_abs[0], inferior_abs[7], linestyle="dashed",lw=0.5)
plt.hlines(inferior_abs[7], d_abs[0], d_abs[7], linestyle="dashed",lw=0.5)
plt.axhline(34.7,linestyle="dashed",lw=0.5,color='red',label='average shooting percentage')
plt.xlim((1,16))
plt.ylim((min(inferior_abs),37))
plt.legend()
plt.xlabel(r'$\delta_{space}^*(0)$ inferior to [feet]')
plt.ylabel(r'3-point shooting percentage')
plt.plot(d_abs,inferior_abs)
print(inferior_abs[3],inferior_abs[7])
25.965488907148725 33.121491367439894
fig,ax=plt.subplots(1,1,figsize=(4.5,3))
d_abs=[1*(i+1) for i in range(16)]
inferior_abs=[]
df_shots.loc[10538,'Shot_result']=0
df_shots.loc[15772,'Shot_result']=0
for d in d_abs:
l=[7641,14919]
#if d==2:
# print(df_shots.query("1<d_release<@d and btpl==True and Shot_result==1 and shot_id not in @l")[['shot_id']])
m=df_shots.query("d_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[0,'D']
s=df_shots.query("d_release<@d and btpl==True and shot_id not in @l").groupby('Shot_result').count().loc[1,'D']
inferior_abs.append(s/(s+m)*100)
plt.vlines(d_abs[3], inferior_abs[0], inferior_abs[3], linestyle="dashed",lw=0.5)
plt.hlines(inferior_abs[3], d_abs[0], d_abs[3], linestyle="dashed",lw=0.5)
plt.vlines(d_abs[7], inferior_abs[0], inferior_abs[7], linestyle="dashed",lw=0.5)
plt.hlines(inferior_abs[7], d_abs[0], d_abs[7], linestyle="dashed",lw=0.5)
plt.axhline(34.7,linestyle="dashed",lw=0.5,color='red',label='average shooting percentage')
plt.xlim((1,16))
plt.ylim((min(inferior_abs),37))
plt.legend()
plt.xlabel(r'$\delta_{space}^*(0)$ inferior to [feet]')
plt.ylabel(r'3-point shooting percentage')
plt.plot(d_abs,inferior_abs)
print(inferior_abs[3],inferior_abs[7])
25.965488907148725 33.121491367439894