Introduction: Plotting with Plotly + Cufflinks in Python

In this notebook, we will see how to use plotly and cufflinks to create stunning, interactive figures in a single line of Python. This combination of libraries is simple to use, makes excellent charts, and, in my opinion, much more efficient than other methods of plotting in Python.

This introduction will show us the basics of using plotly + cufflinks, focusing on what we can do in one line of code (for the most part). We'll see a wide variety of different charts, but won't get into the full capabilities of the library (such as updatable figures and using IPython widgets).

Before you get started, you'll want to head to Plotly and create an account to get a username and api key. Then run the code below (this isn't actually necessary if you stick to offline plotting).

import plotly 
# Replace with your information'DemoAccount', api_key='lr1c37zw81')

The Plotly Python library is an open-source version of the Plotly visualization software made by Plotly. You can create a free account and upload your graphs to share with others (this requires making the graphs and data public).

We will run plotly completely in offline mode which means that we won't be publishing any of our graphs online. However, we can still open up any of the plots in the Plotly chart studio and edit them for a presentation.

Cufflinks is a wrapper around the plotly library specifically for plotting with Pandas dataframes. With cufflinks, we don't have to dig into the details of plotly, instead building our charts with minimal code. Basically, you can make charts directly in plotly for more control, or you can use cufflinks to rapidly prototype plots and explore the data.

In [1]:
# plotly standard imports
import plotly.graph_objs as go
import plotly.plotly as py

# Cufflinks wrapper on plotly
import cufflinks

# Data science imports
import pandas as pd
import numpy as np

# Options for pandas
pd.options.display.max_columns = 30

# Display all cell outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

We'll be using plotly + cufflinks in offline mode. We will also set the global cufflinks theme to pearl. Later we'll take a look at the other theme options.

In [2]:
from plotly.offline import iplot

# Set global theme
cufflinks.set_config_file(world_readable=True, theme='pearl')