Show Sidebar Hide Sidebar

Offline Plots in Plotly in Python

How to use Plotly offline inside IPython notebooks

New to Plotly?¶

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check¶

Plotly Offline brings interactive Plotly graphs to the offline (local) environment. Instead of saving the graphs to a server, your data and graphs will remain in your local system. When your ready to share, you can just publish them to the web with an online Plotly account or to your company's internal Plotly Enterprise.
To get started with Plotly Offline, upgrade to the 1.9.x series or higher:

In [1]:
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

print __version__ # requires version >= 1.9.0
1.12.9

Plotly Offline from Command Line¶

You can plot your graphs from a python script from command line. On executing the script, it will open a web browser with your Plotly Graph drawn.

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

plot([Scatter(x=[1, 2, 3], y=[3, 1, 6])])
Out[2]:
'file:///Users/Chelsea/Repos/documentation/_posts/python/offline/temp-plot.html'

Generating Offline Graphs within Jupyter Notebook¶

You can also plot your graphs offline inside a Jupyter Notebook Environment. First you need to initiate the Plotly Notebook mode as below:

In [3]:
init_notebook_mode(connected=True)

Run at the start of every ipython notebook to use plotly.offline. This injects the plotly.js source files into the notebook.

In [4]:
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
In [6]:
from plotly.graph_objs import *
import numpy as np

x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
       Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
In [8]:
import cufflinks as cf
iplot(cf.datagen.lines().iplot(asFigure=True,
                               kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))

Use with the Cloud¶

All methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise.
get_figure downloads a figure from plot.ly or Plotly Enterprise.
You need to provide credentials to download figures: https://plot.ly/python/getting-started/

In [12]:
import plotly.plotly as py 

fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True)
iplot(fig)

Static Image Export¶

The graphs generated from Plotly Offline can also be saved as an image when working in an ipython notebook, as shown below:

In [13]:
import plotly.offline as offline
import plotly.graph_objs as go

offline.init_notebook_mode()

offline.iplot({'data': [{'y': [4, 2, 3, 4]}], 
               'layout': {'title': 'Test Plot', 
                          'font': dict(size=16)}},
             image='png')