Plotly Python: An Introduction

Bryan White
Jan 11, 2020 · 5 min read

While learning Python, I found countless articles going over the two core graphing libraries — Matplotlib and Seaborn.
However, I found that they lacked some of the ‘professional’ level aesthetic that I was looking for.

I ultimately found Plotly (and Dash) and quickly realized the power, flexibility, and overall ease of use.

In this introductory article, I want to give you an overview of the Plotly library using a sample Kaggle dataset.
I will do my coding via Google Colab notebook. If you are not familiar with Colab, you can refer to an overview article written here.

What is Plotly?

Plotly Python is an open-source library built on top of plotly.js which allows users to create professional quality, interactive, web-based or standalone visualizations or applications.
Visualizations can be displayed in Jupyter notebooks, standalone HTML files, or integrated into web-applications via the Dash framework.

Over 40 unique charts and limitless customization options exist across statistical, financial, geographic, scientific, and 3-D plot types.

Getting Started

I’ve decided to pull an ice hockey dataset from Kaggle to use as my example.
I loaded the dataset into pandas and did a subset of the data just for the year 2018, which can be seen as below:

Let’s say we’re interested in seeing a distribution of points by player. For this dataset, a histogram would be a great starting point. To create a histogram in Plotly, you first create a Figure, telling Plotly what data to use:

import plotly.graph_objects as gofig = go.Figure(data=[go.Histogram(x=hockeydf_2018['PTS'])])fig.show()

We import the plotly.graph_objects library as go and use it to create a new Figure — which by naming convention we will call fig.

We pass in the data parameter (which is also known as the Trace) to tell Plotly what we want to plot, as well as what type of plot.

So here we see a histogram displaying the distribution of points per player. Very easy to create, but also very basic. Plotly uses a default Layout definition when it generates the plot, but what if we want to customize it further?

How about we change the number of bins that we sort the data into?

fig = go.Figure(data=[go.Histogram(
x=hockeydf_2018['PTS'],
xbins=dict(start=0,
end=120,
size=5))
])
fig.show()

You can start to see how you can easily add elements to a graph to customize it further.
It’s common to use whitespace within the plot definitions to make your code easier to read — and easier to edit later on.

Now, what if we also wanted to add some labels and a title to our histogram?

fig = go.Figure(data=[go.Histogram(
x=hockeydf_2018['PTS'],
xbins=dict(start=0,
end=120,
size=5))
])
fig.update_layout(
title_text='2018 NHL Points by Player',
xaxis_title_text='Value',
yaxis_title_text='Count',
)
fig.show()

This time, we used the fig.update_layout function to directly add titles to our graph. The updated graph can be seen below:

What about adding additional data (traces) to our plot? It could be interesting to see how the distribution of points varies over time.
I have gone ahead and made two other dataframes which contain players point information for 2000, and 1980.
Point totals likely won’t change too drastically from one year to the next, but if we look at a longer time interval we may see some more interesting trends emerge.

To add these to our plot, you would use the fig.add_trace function. We also need to make a small change to our layout to overlay the histograms and adjust the trace opacity for better visibility:

fig = go.Figure(data=[go.Histogram(
x=hockeydf_2018['PTS'],
name='2018',
xbins=dict(start=0,
end=120,
size=5))
])
fig.add_trace(go.Histogram(x=hockeydf_2000['PTS'], name='2000'))
fig.add_trace(go.Histogram(x=hockeydf_1980['PTS'], name='1980'))
fig.update_layout(
title_text='NHL Points by Player - 1980, 2000, 2018',
xaxis_title_text='Value',
yaxis_title_text='Count',
barmode='overlay'
)
fig.update_traces(opacity=0.4)
fig.show()

It’s as easy as adding options to update_layout, update_traces, or adding data with add_trace.
Our plot now looks like this:

You can quickly see some interesting trends coming out of our comparisons now!
The 1980’s seemed to have a lot more dominant players who were scoring on the high side of the points spectrum, while more recent years we see that the distribution is moved much more to the left, with more players scoring fewer points.

How about rather than overlaying, do a side-by-side comparison?
Using the subplots library:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=3,
subplot_titles=('2018', '2000', '1980')
)
trace1 = go.Histogram(x=hockeydf_2018['PTS'])
trace2 = go.Histogram(x=hockeydf_2000['PTS'])
trace3 = go.Histogram(x=hockeydf_1980['PTS'])
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)
fig.update_layout(showlegend=False)fig.show()

Here, we create 3 datasets (traces) which we then append onto fig (the plot).
When we append, we have to choose which row, column position to place the trace in.
In the above code, we created a 1 row, 3 column subplot, so when we append traces we use the (row, column) designation — (1,1), (1,2), (1,3).
Our subplot histogram now looks like:

Pretty cool!

There is an endless amount of customization we can apply — from fonts, bar colors, line/grid styling, to adding annotations and interactive widgets to our plots.

I hope this shows just have easy it is to get start with Plotly, and stay tuned for more guides on some more advanced topics soon.

Photo by Clark Tibbs on Unsplash

The Startup

Get smarter at building your thing. Join The Startup’s +799K followers.

Bryan White

Written by

Supply Chain Analyst and Data Science Student at the University of Auckland.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +799K followers.

Bryan White

Written by

Supply Chain Analyst and Data Science Student at the University of Auckland.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +799K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store