text_html_output_1.png
from plotly.offline import iplot, init_notebook_mode

import numpy as np

import pandas as pd

import pandas as pd
import numpy as np
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()
data = pd.read_csv('../input/shot_logs.csv')
playerIDList = list(data['player_id'].unique())
defenderIDList = list(data['CLOSEST_DEFENDER_PLAYER_ID'].unique())
attackerIDList = list(data['player_id'].unique())
playerData = pd.DataFrame(index=playerIDList, columns=['player', 'made', 'missed', 'fg_percentage', 'made_against', 'missed_against', 'fg_percentage_against'])
for defenderID in defenderIDList:
    name = data[data['CLOSEST_DEFENDER_PLAYER_ID'] == defenderID]['CLOSEST_DEFENDER'].iloc[0]
    made = np.sum(data[data['CLOSEST_DEFENDER_PLAYER_ID'] == defenderID]['SHOT_RESULT'] == 'made')
    missed = np.sum(data[data['CLOSEST_DEFENDER_PLAYER_ID'] == defenderID]['SHOT_RESULT'] == 'missed')
    percentage = made / (made + missed)
    playerData.at[defenderID, 'player'] = name
    playerData.at[defenderID, 'made_against'] = made
    playerData.at[defenderID, 'missed_against'] = missed
    playerData.at[defenderID, 'fg_percentage_against'] = percentage
for attackerID in attackerIDList:
    made = np.sum(data[data['player_id'] == attackerID]['SHOT_RESULT'] == 'made')
    missed = np.sum(data[data['player_id'] == attackerID]['SHOT_RESULT'] == 'missed')
    percentage = made / (made + missed)
    playerData.at[attackerID, 'made'] = made
    playerData.at[attackerID, 'missed'] = missed
    playerData.at[attackerID, 'fg_percentage'] = percentage
newPlayerData = playerData.sort_values('fg_percentage_against')
newPlayerData2 = newPlayerData.drop(newPlayerData[newPlayerData.missed_against < 200].index)

ESPNRankTop30 = ['James, LeBron', 'Paul, Chris', 'Davis, Anthony', 'Westbrook, Russell', 'Griffin, Blake', 'Curry, Stephen', 'Love, Kevin', 'Durant, Kevin', 'Harden, James', 'Howard, Dwight', 'Anthony, Carmelo', 'Noah, Joakim', 'Aldridge, LaMarcus', 'Gasol, Marc', 'Parker, Tony', 'Lillard, Damian', 'Nowitzki, Dirk', 'Wall, John', 'Cousins, DeMarcus', 'Bosh, Chris', 'Duncan, Tim', 'Jefferson, Al', 'Irving, Kyrie', 'Leonard, Kawhi', 'Ibaka, Serge', 'Horford, Al', 'Dragic, Goran', 'Rose, Derrick', 'Lowry, Kyle', 'Drummond, Andre']
newPlayerData3 = newPlayerData[newPlayerData['player'].isin(ESPNRankTop30)]
newPlayerData3 = newPlayerData3.sort_values('player')
newPlayerData3['ranking'] = 0
for i in range(len(ESPNRankTop30)):
    newPlayerData3.loc[newPlayerData3['player'] == ESPNRankTop30[i], 'ranking'] = str(i + 1)

line = Scatter(x=[0, 1], y=[0, 1], marker=dict(size=1, color='rgba(200, 200, 200, .5)'), name='Line of Neutrality')
trace1 = Scatter(x=newPlayerData2['fg_percentage'], y=newPlayerData2['fg_percentage_against'], mode='markers', marker=dict(size=10, color='rgba(132, 123, 255, .9)', line=dict(width=2)), name='League', text=newPlayerData2['player'])
trace2 = Scatter(x=newPlayerData3['fg_percentage'], y=newPlayerData3['fg_percentage_against'], mode='markers', marker=dict(size=10, color='rgba(255, 123, 132, .9)', line=dict(width=2)), name='#NBARank Top 30', text=newPlayerData3['player'] + ' (#' + newPlayerData3['ranking'] + ')')
data = [line, trace1, trace2]
layout = Layout(hovermode='closest', annotations=Annotations([Annotation(x=0.5004254919715793, y=-0.16191064079952971, showarrow=False, text='Made Field Goal %', xref='paper', yref='paper'), Annotation(x=-0.05944728761514841, y=0.4714285714285711, showarrow=False, text='Allowed Field Goal %', textangle=-90, xref='paper', yref='paper')]), autosize=True, margin=Margin(b=100), title='Made Vs. Allowed FG%', xaxis=XAxis(autorange=False, range=[0.35, 0.72], type='linear'), yaxis=YAxis(autorange=False, range=[0.35, 0.55], type='linear'))
graph = Figure(data=data, layout=layout)

data = pd.read_csv('../input/shot_logs.csv')
defenderIDList = list(data['CLOSEST_DEFENDER_PLAYER_ID'].unique())
attackerIDList = list(data['player_id'].unique())
playerData = pd.DataFrame(index=playerIDList, columns=['player', 'made', 'missed', 'fg_percentage', 'fg_distance'])
for attackerID in attackerIDList:
    name = data[data['player_id'] == attackerID]['player_name'].iloc[0]
    spacePos = name.find(' ')
    firstname = name[0].upper() + name[1:spacePos]
    lastname = name[spacePos + 1].upper() + name[spacePos + 2:]
    name = firstname + ' ' + lastname
    made = np.sum(data[data['player_id'] == attackerID]['SHOT_RESULT'] == 'made')
    missed = np.sum(data[data['player_id'] == attackerID]['SHOT_RESULT'] == 'missed')
    percentage = made / (made + missed)
    averageDist = np.mean(data[data['player_id'] == attackerID]['SHOT_DIST'])
    playerData.at[attackerID, 'player'] = name
    playerData.at[attackerID, 'made'] = made
    playerData.at[attackerID, 'missed'] = missed
    playerData.at[attackerID, 'fg_percentage'] = percentage
    playerData.at[attackerID, 'fg_distance'] = averageDist
newPlayerData = playerData.sort_values('fg_distance', ascending=False)
newPlayerData2 = newPlayerData.drop(newPlayerData[newPlayerData.made < 200].index)
newPlayerData2

import plotly
import plotly.plotly as py
from plotly.graph_objs import *
from ipywidgets import widgets
from IPython.display import display, clear_output, Image
from plotly.graph_objs import *
from plotly.widgets import GraphWidget
ESPNRankTop30 = ['Lebron James', 'Chris Paul', 'Anthony Davis', 'Russell Westbrook', 'Blake Griffin', 'Stephen Curry', 'Kevin Love', 'Kevin Durant', 'James Harden', 'Dwight Howard', 'Carmelo Anthony', 'Joakim Noah', 'Lamarcus Aldridge', 'Marc Gasol', 'Tony Parker', 'Damian Lillard', 'Dirk Nowtizski', 'John Wall', 'Demarcus Cousins', 'Chris Bosh', 'Tim Duncan', 'Al Jefferson', 'Kyrie Irving', 'Kawhi Leonard', 'Serge Ibaka', 'Al Horford', 'Goran Dragic', 'Derrick Rose', 'Kyle Lowry', 'Andre Drummond']
trace1 = Scatter(x=newPlayerData2['fg_distance'], y=newPlayerData2['fg_percentage'], mode='markers', marker=dict(size=newPlayerData2['made'] / 20, color='rgba(132, 123, 255, .9)', line=dict(width=2)), name='League', text=newPlayerData2['player'])
newPlayerData3 = newPlayerData2[newPlayerData2.player.isin(ESPNRankTop30)]
trace2 = Scatter(x=newPlayerData3['fg_distance'], y=newPlayerData3['fg_percentage'], mode='markers', marker=dict(size=newPlayerData3['made'] / 20, color='rgba(255, 123, 132, .9)', line=dict(width=2)), name='#NBARank Top 30', text=newPlayerData3['player'])
data = [trace1, trace2]
layout = Layout(hovermode='closest', annotations=Annotations([Annotation(x=0.5004254919715793, y=-0.16191064079952971, showarrow=False, text='Average Shot Distance (Feet)', xref='paper', yref='paper'), Annotation(x=-0.06944728761514841, y=0.4714285714285711, showarrow=False, text='Field Goal %', textangle=-90, xref='paper', yref='paper')]), autosize=True, margin=Margin(b=100), title="Comparing Players' FG% and Average Shot Distance (Minimum 200 Made Shots)")
graph = Figure(data=data, layout=layout)
iplot(graph)