GithubHelp home page GithubHelp logo

datadesk / kobe-every-shot-ever Goto Github PK

View Code? Open in Web Editor NEW
66.0 11.0 11.0 2.63 MB

A Los Angeles Times analysis of Every shot in Kobe Bryant's NBA career

Home Page: http://graphics.latimes.com/kobe-every-shot-ever/

Jupyter Notebook 100.00%
python pandas jupyter-notebook news data-journalism journalism data-analysis sports

kobe-every-shot-ever's Introduction

kobe-every-shot-ever

The code behind http://graphics.latimes.com/kobe-every-shot-ever/

This graphic uses data from stats.nba.com to plot all the shots Kobe Bryant ever took during NBA games (except two shots that are mysteriously missing). It uses Pandas, Matplotlib, CartoDB and Leaflet to scrape and display the data.

The Python code is in a Jupyter notebook in this repository. You can view it here on Github or run it yourself if you have Jupyter installed. To install all the necessary libraries:

$ pip install -r requirements.txt

Once we scraped the data, we exported a csv and translated the x and y coordinates to latitude and longitude.

#####Learn from our mistakes

We did a very simple translation from X and Y coordinates to latitude and longitude. We opened the data in Excel and added two extra columns that added the X and Y values (divided by 1000) to the latitude and longitude of the Staples Center.

This solution is what we came up with on a deadline — and it worked. But next time, we'd do it differently. Here's why:

• One degree of latitude does not cover the same distance as one degree of longitude. So our court ended up slightly stretched vertically.

• Opening the data in Excel is a non-programmatic step that is hard to automate. It would have been a lot easier to just add the new columns in Pandas.

• It's fun to center the hoop on the Staples Center, but in our case it added nothing to the project. We should have just centered the map at 0,0 to make it easier to work with.

#####Get it in CartoDB After adding lat and lon, we uploaded the data to CartoDB. Then it was time to display it in Leaflet. Here's the magic snippet:

cartodb.createLayer(mapchart, {
    id:"6eadec2c-0204-11e6-865f-0e674067d321",
    user_name: 'latimes',
    type: 'cartodb',
    cartodb_logo: false,
    sublayers: [{
      sql: "SELECT * FROM kobe_all_shots_geocoded_final_merge ORDER BY priority,event_type DESC",
      layer_name: "allshots",
      cartocss: cssString,
      interactivity: "combined_shot_type,action_type,season,event_type,shot_distance,playoffs,opponent,game_date",
      attribution: "Source: stats.nba.com"
    }]

  });

cartodb.createLayer is a method of cartodb.js that lets you quickly create a leaflet layer from a CartoDB dataset. It uses CartoCSS for styling. Here's an example of that:

var cssString = "#kobe_all_shots_geocoded_final_merge{\
    marker-fill-opacity: 0.4;\
    marker-line-color: #FFF;\
    marker-line-width: 1;\
    marker-line-opacity: 0;\
    marker-placement: point;\
    marker-type: ellipse;\
    marker-width: 10;\
    [event_type = 'Made Shot']{\
        marker-fill: #6a3a89;\
    }\
      [event_type = 'Missed Shot']{\
        marker-fill: #F5D05E;\
        marker-line-width: 0.5;\
        marker-fill-opacity: 0.5;\
        marker-line-color: #cacaca;\
        marker-line-opacity: 0.6\
    }\
    marker-allow-overlap: true;\
     [zoom = 16] { \
       marker-width: " + baseMarkerWidth * 24 +"; \
     } \
    [zoom = 15] { \
       marker-width: " + baseMarkerWidth * 17 +"; \
     } \
    [zoom = 14] { \
       marker-width: " + baseMarkerWidth * 10 +"; \
     } \
     [zoom = 13] { \
       marker-width: " + baseMarkerWidth * 6 +"; \
     } \
     [zoom = 12] { \
       marker-width: " + baseMarkerWidth * 4 + "; \
     } \
    [zoom = 11] { \
       marker-width: " + baseMarkerWidth * 2 + "; \
     } \
    [zoom = 10] { \
       marker-width: " + baseMarkerWidth + "; \
     } \
}";

kobe-every-shot-ever's People

Contributors

joemfox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kobe-every-shot-ever's Issues

Link to the graphic doesn't work from the EU

Clicking the link redirects me to a page reading:

While most of our pages are available in a version of latimes.com created for European Union users, some are currently unavailable. We are engaged on the issue and committed to identifying technical compliance solutions to this problem. Thanks for your interest in the Los Angeles Times.

Not really an issue with the code, but could still be solved here by embedding (a smaller version of) the graphic within README.md, don't you agree?

RIP

I can't believe this truth,never forget kobe,RIP Mamba

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.