An Introduction to Plotly
The main visualization/plotting tool I use is Plotly. This plotting platform works with many different programming languages, but I, of course, mostly use it with Python. Here’s a quick primer showing you how to make your first plot in Plotly!
Plotly works well in conjunction with pandas through the use of a package called cufflinks. We’ll need to install plotly and cufflinks via pip:
pip install plotly pip install cufflinks
Beyond that, you’ll have some more set-up to login to your account. This is very well explained on Plotly’s website
I want to plot LeBron James’ 2016 Season, and let’s plot a 5 day moving average so we can cut down on the game to game noise. You can download the data here by clicking on the export button above the data set.
On to the code. All of this is pretty simple stuff. We need to do some initial cleaning of the data, then I change it into Moving 5 day averages instead of the raw data, then we plot!
1 2 3 4 5 6 7 8 9 10 11 12 13 14
df_lbj = pd.read_csv('/Download_Path/players_j_jamesle01_gamelog_2016__pgl_basic.csv') # They have some weird rows you need to ignore df = df_lbj[cols][(df_lbj['PTS']!='Did Not Play')&(df_lbj['PTS']!='Inactive')&(df_lbj['PTS']!='PTS')] # Get the rolling mean of the latest 5 games for each column for column in df.columns: df[column] = df[column].rolling(window=5, min_periods=3).mean() # For reading sake, let's *100 the two percentage categories df['FG%'] = df['FG%']*100 df['3P%'] = df['3P%']*100 # Now Plot it! This is the easy part # Title is your graph title, fill makes the lines "filled" as you'll see below df.iplot(filename='LBJ Moving Average', fill=True) # Do you prefer subplots? That's easy too! df.iplot(filename='LBJ Moving Average Subplots', fill=True, title='LBJ Last 5 Games MA', subplots=True)
And here are your plots!