Getting Started with Plot.ly
By: Ishaan Dey
Tags: Data Visualization, Plotly, Data Science, Python

Getting Started with Plot.ly

A Guided Walkthrough for Powerful Visualizations in Python

Authors: Elyse Lee and Ishaan Dey

Matplotlib is alright, Seaborn is great, but Plot.ly? That’s on an entirely new level. Plot.ly offers more than your average graph by providing options for full interactivity and many editing tools. Differentiated from the others by having options to have graphs in offline and online mode, it is also equipped with a robust API that when set up will work seamlessly to have the graphs displayed in a web browser as well as the ability for saving a local copy. One of the only frustration you’ll come across is dealing with many options to figure out the tools you want to use for your plots.

Overview

We’ll start with the basics of setting up plot.ly in Python. After that, we’ll get started with some basic visualizations ranging from typical box & whisker plots to choropleth maps, with code breakdowns along the way. We’ve made all visualizations in this guide using the Zillow Economics Dataset, which contains time-series data from 1996 to 2017 on various housing metrics aggregated by location. If you’re interested in the full code for this post, check out the GitHub link below; otherwise, all the code used to create the visualizations will be included for each visualization.

Our hope is that by the end, you’ll have developed a basic intuition for how the plotly API works, as well as a feel for the generalizable framework you can apply towards your own projects. You can find a link to a plotly cheatsheet here, and if you’re interested in fine tuning any of the parameters used for the visualization, you can access the documentation using the help() function. For more details on all types of plots and parameters, here is a link to more information on Plotly’s Python open source graphing library.

Setting Up

Plotly is a platform that runs on JSON, a format in which parameters are passed to the plotly API in dictionary formats. We can access this API in python using the plot.ly package. To install the package, open up terminal and type $ pip install plotly or $ sudo pip install plotly .

Plotly’s graphs are hosted using an online web service, so you’ll first have to setup a free account online to store your plots. To retrieve your personal API key, follow the link here: https://plot.ly/settings/api#/. Once you’ve done so, you can begin setting up plotly with the set_credential_files() function, as shown below.

import plotly plotly.tools.set_credentials_file(username=’YourAccountName’, api_key=’YourAPIKey’)``

Plotting Online & Offline

When displaying visualizations on plotly, both the plot and data are saved to your plotly account. Without paying for more space in cloud, you’ll have a maximum of 25 plots that can be stored on the cloud, but these images can easily be stored locally and deleted when making space for more.

There are two main ways to display plotly plots. If you’re using Jupyter Notebook or another interactive python environment (files with the .ipynb extension), the py.iplot() function displays the plots in the output below the cell. py.plot() , on the other hand, returns a url that can be saved, and also opens using the default web browser.

The Plotly offline mode also enables you to save graphs locally. To plot offline, you can use plotly.offline.plot() or plotly.offline.iplot() . Again, the iplot() function is used for Jupyter notebook, and will display the plots within the notebook. plot() creates an HTML page that is saved locally to be opened in a web browser.

Basic Structure

As we mentioned before, all plot.ly visualizations are created using Json structure which are list of parameters to be modified using API, so essentially you’ll see the parameters and general structure to make each plot which if you learn one, you can make the rest.

import plotly.plotly as py

import plotly.graph_objs as go

import plotly.plotly as py : This has the functions for communicating with the plotly servers

import plotly.graph_objs as go : This has the functions for generating graph objects. This is a useful module for calling help on to see all the attributes taken as parameters of an object. There are also different useful methods of the object available such as the update method that can be used to update the plot object to add more information onto it.

Generalized Structure

The graph_objs class contains several structures that are consistent across visualizations made in plot.ly, regardless of type.

We begin with trace , which can be thought of as an individual layer that contains the data and specifications for how the data should be plotted (i.e. lines, markers, chart type). Here’s an example of the structure of trace:

trace1 = {

"x": ["2017-09-30", "2017-10-31", "2017-11-30", ...],

"y": [327900.0, 329100.0, 331300.0, ...],

"line": {

"color": "#385965",

"width": 1.5

},

"mode": "lines",

"name": "Hawaii",

"type": "scatter",

}

As you can see, trace is a dictionary of parameters of the data to be plotted, as well as information about the color and line types.

We can compile several traces by appending them to a list, which we’ll call data . The order of traces in the list determine the order in which they’re laid onto the final plot. Typically, data should look something like this:

data = [trace1, trace2, trace3, trace4]

layout = go.Layout() : This object is used for the layout of the data including how it looks and changeable features such as title, axis titles, font, and spacing. Just like trace , it is a dictionary of dictionaries.

layout = {

"showlegend": True,

"title": {"text": "Zillow Home Value Index for Top 5 States"},

"xaxis": {

"rangeslider": {"visible": True},

"title": {"text": "Year from 1996 to 2017"},

"zeroline": False

},

"yaxis": {

"title": {"text": "ZHVI BottomTier"},

"zeroline": False

}

}

We can finally compile the data and the layout using the go.Figure() function, which eventually gets passed to the plotting function that we choose.

fig = go.Figure(data = data, layout = layout)

Bar Chart

go.Bar() creates a bar chart type figure. Within the go.Layout() function, we can specify important information such as barmode = “group” , which groups the different bars for each year together, labels for the x and y axes, and a title for the full graph.

Line Plot

go.Scatter() instantiates a trace of scatter type, as opposed to a bar chart or other form.

We can change the mode of the marker using the mode parameter. Even though we are using a scatter plot, we can generate a scatter plot which creates lines and markers (points) on the lines.

mode = “lines+markers”

Time Series Line Plot

Here, we’ve added a range slider that adjusts the domain of data that can be included in the main plot using the rangeslider parameter.

We’ve also passed a colors dictionary containing a unique color for each state. To do so, we used the seaborn color_palette() function, specified the color range, as well as the number of discrete values we need from the distribution. Because plot.ly will not accept RGB tuples, we can convert the output to HEX codes using the as_hex() function.

Multiple Scatter Plots

To create this layout, instead of appending the traces to a single dictionary, we create subplots using the make_subplots() function, and add the trace to a specific location on the grid using the append_trace() function.

Choropleth Map

With the choropleth, we can take a shortcut using the figure factory class, which contains a set of functions to easily plot more complex figures such geographical maps.

import plotly.figure_factory as ff

From the ff.create_choropleth() function, we pass a set of FIPS values, or geographical identification codes specific to each county, city, or state, where the values (ZHVI_BottomTier) correspond to the data to be assigned to that region.

Final Thoughts

As depicted from the examples of different types of graphs above, Plot.ly is a powerful tool for developing both visually pleasing and comprehensible plots for a wide range of audiences. It has many benefits including being widely accessible with having both offline and online modes, and containing functions that can display generated graphs in the notebook and in a web browser. With extra advantages in interactivity, Plotly is a great alternative to Matplotlib and Seaborn, and can boost impact for presentation.

Let us know if you have any questions!