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

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

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]:

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

Use with the Cloud¶

All methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise.
get_figure downloads a figure from or Plotly Enterprise.
You need to provide credentials to download figures:

In [12]:
import plotly.plotly as py 

fig = py.get_figure('', raw=True)

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.iplot({'data': [{'y': [4, 2, 3, 4]}], 
               'layout': {'title': 'Test Plot', 
                          'font': dict(size=16)}},