Skip to content

Data Visualization - Plotly and Cufflinks

Plotly is a library that allows you to create interactive plots that you can use in dashboards or websites (you can save them as html files or static images). Cufflinks allows us to call plotly plots from pandas dataframes, so provides the link between pandas and plotly.

It is likely you will need to install the plotly and cufflinks packages. A quick online search will show you how to do this for your system.

import pandas as pd
import numpy as np
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

print(__version__) # requires version >= 1.9.0

Make sure you are running a version that is at least 1.9.0.

import cufflinks as cf
# connect javascript to the notebook
init_notebook_mode(connected=True)
# For offline use (plotly is a company that charges for online hosting)
cf.go_offline()

Examples

df = pd.DataFrame(np.random.randn(100,4),columns='A B C D'.split())
df.head()

Graph Graph

df.iplot(kind='scatter',x='A',y='B',mode='markers',size=10)

Graph Graph

df2.iplot(kind='bar',x='Category',y='Values')

Graph Graph

df.count().iplot(kind='bar')

Graph Graph

df.iplot(kind='box')

Graph Graph

df[['A','B']].iplot(kind='spread')

Graph Graph

df['A'].iplot(kind='hist',bins=25)

Graph Graph

df.iplot(kind='bubble',x='A',y='B',size='C')

Graph Graph

df.scatter_matrix()

Graph Graph


Exercises