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.__version__
Out[1]:
'3.6.1'

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

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 Chart Studio Cloud or Chart Studio Enterprise.
get_figure downloads a figure from plot.ly or Chart Studio Enterprise.
You need to provide credentials to download figures: https://plot.ly/python/getting-started/

In [7]:
import plotly.plotly as py 

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

Static Image Export

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

In [8]:
import plotly.io 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

Image(static_image_bytes)
Out[9]:

Use plotly.io.write_image 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')

Reference

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