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).

Installation

In order for this all to work, you'll need to install plotly and cufflinks to call plots directly off of a pandas dataframe. These libraries are not currently available through conda but are available through pip. Install the libraries at your command line/terminal using:

Using Cufflinks and iplot()

    • scatter

    • bar

    • box

    • spread

    • ratio

    • heatmap

    • surface

    • histogram

    • bubble

Install

pip install plotly

pip install cufflinks

IMPORT AND SETUP

import pandas as pd

import numpy as np

%matplotlib inline

Check Version

from plotly import __version__ from

plotly.offline import download_plotlyjs, init_notebook_mode, plot, pilot

print(__version__) # requires version >= 1.9.0

import cufflinks as cf

# For Notebooks

init_notebook_mode(connected=True)

# For offline use

cf.go_offline()

Fake Data

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

df2 = pd.DataFrame({'Category':['A','B','C'],'Values':[32,43,50]})

df.iplot()

SCATTER

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

Bar Plots

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

Boxplots

df.iplot(kind='box')

3d Surface

df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})

df3.iplot(kind='surface',colorscale='rdylbu')

Spread

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

histogram

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

for bubble plot

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

scatter_matrix()

Similar to sns.pairplot()

df.scatter_matrix()

Choropleth Maps

Offline Plotly Usage

Get imports and set everything up to be working offline.

import plotly.plotly as py

import plotly.graph_objs as go

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, pilot

#To show figure in the notebook

init_notebook_mode(connected=True)

Choropleth US Maps

Plotly's mapping can be a bit hard to get used to at first, remember to reference the cheat sheet in the data visualization folder, or [find it online here](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf)

import pandas as pd

Now we need to begin to build our data dictionary. Easiest way to do this is to use the dict() function of the general form:

    • type = 'choropleth',

    • locations = list of states

    • locationmode = 'USA-states'

    • colorscale=

Either a predefined string:

'pairs' | 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | 'Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'

or create a custom scale custom colorscale

    • text= list or array of text to display per point

    • z= array of values on z axis (color of state)

    • colorbar = {'title':'Colorbar Title'})

data = dict(type = 'choropleth',

locations = ['AZ','CA','NY'],

locationmode = 'USA-states',

colorscale= 'Portland',

text= ['text1','text2','text3'],

z=[1.0,2.0,3.0],

colorbar = {'title':'Colorbar Title'})

Then we create the layout nested dictionary:

layout = dict(geo = {'scope':'usa'})

Then we use:

go.Figure(data = [data],layout = layout)

to set up the object that finally gets passed into iplot()

choromap = go.Figure(data = [data],layout = layout)

iplot(chromap)

Real Data US Map Choropleth

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")

Now out data dictionary with some extra marker and colorbar arguments:

data = dict(type='choropleth',

colorscale = 'YIOrRd',

locations = df['code'],

z = df['total exports'],

locationmode = 'USA-states',

text = df['text'],

marker = dict(line = dict(color = 'rgb(255,255,255)',width = 2)),

colorbar = {'title':"Millions USD"}

)

And layout dictionary with some more arguments

layout = dict(title = '2011 US Agriculture Exports by State',

geo = dict(scope='usa',

showlakes = True,

lakecolor = 'rgb(85,173,240)')

)

choromap = go.Figure(data = [data],layout = layout)

iplot(chroma)

World Choropleth Map

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

data = dict(

type = 'choropleth',

locations = df['CODE'],

z = df['GDP (BILLIONS)'],

text = df['COUNTRY'],

colorbar = {'title' : 'GDP Billions US'},

)

layout = dict(

title = '2014 Global GDP',

geo = dict(

showframe = False,

projection = {'type':'Mercator'}

)

)

choromap = go.Figure(data = [data],layout = layout)

iplot(choromap)