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's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly

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]:
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

plot([go.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 [5]:
import plotly.graph_objs as go

import numpy as np

x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')),
       go.Scatter(x=x, y=y, mode='markers', marker=dict(color='white', size=3, opacity=0.3))], show_link=False)
In [6]:
import cufflinks as cf


Use with the Cloud

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

In [7]:
import plotly.plotly as py 

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

Static Image Export

The function can then be used to convert a plotly figure to a static image bytes string.

In [8]:
import as pio

static_image_bytes = pio.to_image(fig, format='png')

Use IPython.display.Image to display the image bytes as image in the notebook

In [9]:
from IPython.display import Image


Use to convert a figure to a static image and write it to a file or writeable object.

Make sure to add a file extension or specify the file type using the format parameter.

In [10]:
pio.write_image(fig, file='plotly_static_image.png', format='png')


For more information, run help(plotly.offline.iplot) or help(plotly.offline.plot) or help( or help(