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.
import numpy as np import pandas as pd data = pd.read_csv("FantasyFootball.csv") data.head()
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
#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:
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')
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.