Creating Interactive Charts with Plotly and Python

Python allows you to go beyond static visualisations with interactive graphics that allow you to present more information and get more engagement from your audience. Modules such as plotly and bokeh are the most accessible ways to create these and this article will introduce plotly scatter plots.

Specifcally, this article runs through creating plotly scatter plots if you are working with Python in Jupyter Notebooks. Check out the docs if you are looking to apply these elsewhere. However, the demo in this article will be more than enough to get you up and running with creating an interactive scatter plot that will get end-users engaged with your data!

Before we import plotly, let’s open up our dataset and see what we’re playing with.

In [1]:
import numpy as np
import pandas as pd

data = pd.read_csv("FantasyFootball.csv")
data.head()
Out[1]:
web_name team_code first_name second_name squad_number now_cost selected_by_percent total_points points_per_game minutes element_type total_points_p90 goals_p90 assists_p90 ga_p90
0 Ospina 3 David Ospina 13 4.8 0.2 0 0.0 0 1 0.000000 0.00000 0.0 0.00000
1 Cech 3 Petr Cech 33 5.4 4.9 84 3.7 2070 1 3.652174 0.00000 0.0 0.00000
2 Martinez 3 Damian Emiliano Martinez 26 4.0 0.6 0 0.0 0 1 0.000000 0.00000 0.0 0.00000
3 Koscielny 3 Laurent Koscielny 6 6.0 1.6 76 4.2 1595 2 4.288401 0.00000 0.0 0.00000
4 Mertesacker 3 Per Mertesacker 4 4.8 0.5 15 3.0 351 2 3.846154 0.25641 0.0 0.25641

Our dataset is a fantasy football dataset, with each player represented by a row. The row contains some biographical data, and per 90 performance data for goals, assists and points. Learn more about per 90 data in football here.

Now that we have met our data, let’s import the libraries needed to make our interactive visualisation. Check out the imports below, with comments explaining what each does

In [2]:
#Imports the tools needed to run plotly offline - usually plotly interacts with an online resource.
#These imports allow us to host everything from our computer
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import cufflinks as cf

#Allows us to plot our interactive charts in our Jupyter Notebook
init_notebook_mode(connected=True)
cf.go_offline()

All of these modules allow us to make plotting interactive charts very easy. To start with, we simply run the ‘.iplot()’ method from our dataframe. We then pass it a range of arguments to show exactly what we want:

  • Kind: The type of chart that we want. This time, we’ll use scatter, but there are many to choose from, such as histograms or line charts.
  • X & Y: The axes around which we will plot our data. Below, we’ll use points per 90 and cost.
  • Mode: Our type of scatter plot – we’ll use markers here to plot our data.
  • Text: What text should we show when we hover over a point?
  • Size: How big are our markers?
  • xTitle/yTitle/title: Axis and chart titles.

There are many other things that we could do, such as change fonts and colours, but for a first attempt, let’s see how this looks:

In [6]:
data.iplot(kind='scatter',x='ga_p90',y='now_cost',
           mode='markers',text='web_name',size=10,
          xTitle='Points per 90',yTitle='Cost',title='Cost vs Points p90')

Summary

I think that is pretty cool! We are showing the relationship and distribution – cost tends to go up as points per90 go up. We are also showing some potential value in players that overperform their price. Most impressively, however, users can hover over the chart to get the names of players and more information. This should attract buy-in to our graphics and inform better (& more neatly) than we could with static labels. Great job!

The offline version of plotly is much simpler than the full version, so take this as an introduction. If you are looking to learn more about interactive visualisation, take a read of the examples and docs at plotly and bokeh.