Plotly - Exporting to Static Images



Outputs of offline graphs can be exported to various raster and vector image formats. For that purpose, we need to install two dependencies – orca and psutil.

Orca

Orca stands for Open-source Report Creator App. It is an Electron app that generates images and reports of plotly graphs, dash apps, dashboards from the command line. Orca is the backbone of Plotly's Image Server.

psutil

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization in Python. It implements many functionalities offered by UNIX command line tools such as: ps, top, netstat, ifconfig, who, etc. psutil supports all major operating systems such as Linux, Windows and MacOs

Installation of Orca and psutil

If you are using Anaconda distribution of Python, installation of orca and psutil is very easily done by conda package manager as follows −

conda install -c plotly plotly-orca psutil

Since, orca is not available in PyPi repository. You can instead use npm utility to install it.

npm install -g electron@1.8.4 orca

Use pip to install psutil

pip install psutil

If you are not able to use npm or conda, prebuilt binaries of orca can also be downloaded from the following website which is available at https://github.com/plotly/orca/releases.

To export Figure object to png, jpg or WebP format, first, import plotly.io module

import plotly.io as pio

Now, we can call write_image() function as follows −

pio.write_image(fig, ‘sinewave.png’)
pio.write_image(fig, ‘sinewave.jpeg’)
pio.write_image(fig,’sinewave.webp)

The orca tool also supports exporting plotly to svg, pdf and eps formats.

Pio.write_image(fig, ‘sinewave.svg’)
pio.write_image(fig, ‘sinewave.pdf’)

In Jupyter notebook, the image object obtained by pio.to_image() function can be displayed inline as follows −

Jupyter Notebook Image
Advertisements