plotly_cufflinks_cheat_sheet_1

Plotly is a JS based plotting library with Python bindings (similar to bokeh). Plotly also has a portal online where users can upload their charts for public use.

Installation
pip instal plotly
login
import plotly
plotly.tools.set_credentials_file(username='', api_key='')

create your key here: https://plot.ly/settings/api

Online and Offline plotting

Plotly allows you to plot both

  • online - plot data is set online to your account.
    • plot() - returns a unique url and opens it new tab
    • iplot() - when using notebook to embed the plot
  • offline - explicit call
    • offline.plot - creates a new html output page
    • offline.iplot - interactive while in a notebook
In [2]:
import plotly
In [3]:
plotly.tools.set_credentials_file(username='atmamani',
                                  api_key='xxx')

Online plotting

plot command
In [4]:
import plotly.plotly as py
from plotly.graph_objs import *

trace0 = Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = Scatter(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])

py.plot(data, filename = 'basic-line2')
Out[4]:
'https://plot.ly/~AtmaMani/2'
iplot command
In [5]:
import plotly.plotly as py
from plotly.graph_objs import *

trace0 = Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = Scatter(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])

py.iplot(data, filename = 'basic-line')
High five! You successfully sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~AtmaMani/0 or inside your plot.ly account where it is named 'basic-line'
Out[5]:

Offline plotting

plot command
In [5]:
import plotly
from plotly.graph_objs import Scatter, Layout

plotly.offline.plot({
    "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
    "layout": Layout(title="hello world")
})
Out[5]:
'file:///Users/atma6951/Documents/code/pychakras/pychakras/python_crash_course/temp-plot.html'
iplot command

You need to set initialization code, similar to matplotlib

In [3]:
import plotly
from plotly.graph_objs import Scatter, Layout

# init code
plotly.offline.init_notebook_mode(connected=True)

plotly.offline.iplot({
    "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
    "layout": Layout(title="hello world")
})

Building dashboards with Plotly Dash

Minimal HTML and no JS. Pure Python based dashboards. More info here: https://plot.ly/dash/