Skip to content

Latest commit

 

History

History
206 lines (154 loc) · 6.49 KB

static-image-export.md

File metadata and controls

206 lines (154 loc) · 6.49 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.1
1.1.6
display_name language name
Python 3
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.7.3
description display_as language layout name order page_type permalink thumbnail
Plotly allows you to save static images of your plots. Save the image to your local computer, or embed it inside your Jupyter notebooks as a static image.
file_settings
python
base
Static Image Export
6
u-guide
python/static-image-export/
thumbnail/static-image-export.png

Static Image Export

It's possible to programmatically export figures as high quality static images while fully offline.

Install Dependencies

Static image generation requires the orca commandline utility and the psutil and requests Python libraries. There are 3 general approach to installing these dependencies.

conda

Using the conda package manager, you can install these dependencies in a single command:

$ conda install -c plotly plotly-orca psutil requests

Note: Even if you do not want to use conda to manage your Python dependencies, it is still useful as a cross platform tool for managing native libraries and command-line utilities (e.g. git, wget, graphviz, boost, gcc, nodejs, cairo, etc.). For this use-case, start with Miniconda (~60MB) and tell the installer to add itself to your system PATH. Then run conda install plotly-orca and the orca executable will be available system wide.

npm + pip

You can use the npm package manager to install orca (and its electron dependency), and then use pip to install psutil:

$ npm install -g electron@1.8.4 orca
$ pip install psutil requests
Standalone Binaries + pip

If you are unable to install conda or npm, you can install orca as a precompiled binary for your operating system. Follow the instructions in the orca README to install orca and add it to your system PATH. Then use pip to install psutil.

$ pip install psutil requests
Externally Hosted Orca + pip

If you already run an instance of orca on your network, there's no need to install it. You can simply point to it in your Python code.

import plotly
# specify URL to Orca's endpoint
plotly.io.orca.config.server_url = "http://localhost:9091"

Note that for this to work, you still need to first requests and psutil.

Create a Figure

Now let's create a simple scatter plot with 100 random points of variying color and size.

import plotly.graph_objects as go
import numpy as np
np.random.seed(1)

N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    mode="markers",
    marker=go.scatter.Marker(
        size=sz,
        color=colors,
        opacity=0.6,
        colorscale="Viridis"
    )
))

fig.show()

Write Image File

The plotly.io.write_image function is used to write an image to a file or file-like python object. You can also use the .write_image graph object figure method.

Let's first create an output directory to store our images

import os

if not os.path.exists("images"):
    os.mkdir("images")

If you are running this notebook live, click to open the output directory so you can examine the images as they are written.

Raster Formats: PNG, JPEG, and WebP

Orca can output figures to several raster image formats including PNG, ...

fig.write_image("images/fig1.png")

JPEG, ...

fig.write_image("images/fig1.jpeg")

and WebP

fig.write_image("images/fig1.webp")

Vector Formats: SVG and PDF...

Orca can also output figures in several vector formats including SVG, ...

fig.write_image("images/fig1.svg")

PDF, ...

fig.write_image("images/fig1.pdf")

and EPS (requires the poppler library)

fig.write_image("images/fig1.eps")

Note: It is important to note that any figures containing WebGL traces (i.e. of type scattergl, heatmapgl, contourgl, scatter3d, surface, mesh3d, scatterpolargl, cone, streamtube, splom, or parcoords) that are exported in a vector format will include encapsulated rasters, instead of vectors, for some parts of the image.

Get Image as Bytes

The plotly.io.to_image function is used to return an image as a bytes object. You can also use the .to_image graph object figure method.

Let convert the figure to a PNG bytes object...

img_bytes = fig.to_image(format="png")

and then display the first 20 bytes.

img_bytes[:20]

Display Bytes as Image Using IPython.display.Image

A bytes object representing a PNG image can be displayed directly in the notebook using the IPython.display.Image class. This also works in the Qt Console for Jupyter!

from IPython.display import Image
Image(img_bytes)

Change Image Dimensions and Scale

In addition to the image format, the to_image and write_image functions provide arguments to specify the image width and height in logical pixels. They also provide a scale parameter that can be used to increase (scale > 1) or decrease (scale < 1) the physical resolution of the resulting image.

img_bytes = fig.to_image(format="png", width=600, height=350, scale=2)
Image(img_bytes)

Summary

In summary, to export high-quality static images from plotly.py, all you need to do is install orca, psutil, and requests and then use the plotly.io.write_image and plotly.io.to_image functions (or the .write_image and .to_image graph object figure methods).

If you want to know more about how the orca integration works, or if you need to troubleshoot an issue, please check out the Orca Management section.