🌻 Plotly.py End of Summer Updates 🌻
Tags: Visualization, Plotly, Data Science

Static Image Export and a New Lead Maintainer

plotly.py is a free, open source Python graphing library written and maintained by the company Plotly. Under the hood, it’s an interface to the open source plotly.js JavaScript graphing library. Get started or view the source on GitHub.

Just two months ago we announced plotly.py 3.0.0 , our biggest release yet. It included first-class Jupyter widget integration, deep validation and typeahead support, and several performance improvements (read more).

Today we’re excited to announce two new developments in the plotly.py library:

Announcement 1. Jon Mease, the open source contributor behind 3.0.0, is stepping up as the lead maintainer of plotly.py. Jon has been working in technical computing and data science for many years. He started working on plotly.py because he wants it all: a library with high quality SVG graphics, GPU acceleration for large datasets, two-way interactivity in Jupyter, standalone dashboard support, and an extensive range of chart types. With Jon at the helm, plotly.py has never been in a better place. Welcome Jon!

Announcement 2. Programmatic static image export is now widely available in plotly.py 3.2.0. Here “programmatic” means the ability to save figures as images from code, without requiring any manual interaction with a web browser (e.g. no save dialog). This has been the most requested feature by the community and seriously broadens the applicability of the library. Thanks to Jon, our community no longer has to choose between interactive graphing and high quality static image exports; with plotly.py you can have it both ways.

Quickstart

Converting a figure to an image is simple and it’s very similar to matplotlib’s savefig :

plotly.io.write_image(fig, file, format=None,

scale=None, width=None, height=None)

fig is a Figure or a compatible dict . This is the central declarative object that describes every aspect of a plotly graph.

is a or a compatible . This is the central declarative object that describes every aspect of a plotly graph. file may be a string referring to a local filesystem path, or a file-like object to be written to. If file is a string, then the file extension is used to infer the image format if possible.

may be a string referring to a local filesystem path, or a file-like object to be written to. If is a string, then the file extension is used to infer the image format if possible. The format may be used to explicitly specify the format, and it is required if file is not a string with a common extension. Supported formats are png , jpeg (jpg extension supported as well), webp , svg , pdf , and eps (with poppler installed).

may be used to explicitly specify the format, and it is required if is not a string with a common extension. Supported formats are , (jpg extension supported as well), , , , and (with poppler installed). scale , width , and height all work as you would expect.

plotly.io.to_image(fig, format=None,

scale=None, width=None, height=None)

This function may be used to return the binary representation of the image directly (no temp files or messing with io.BytesIO !). This can be used in conjunction with IPython.display.Image to display static images directly in the notebook or QtConsole.

Installation

The main plotly.py library can be installed either through pip or through conda :

$ pip install plotly --upgrade $ conda install -c plotly plotly

Image export requires two additional dependencies: orca and psutil . psutil may be installed using pip or conda from the standard repositories, and there are three ways to install orca :

$ conda install -c plotly plotly-orca $ npm install -g electron@1.8.4 orca Download and install the prebuilt binary

Performance

One of the really exciting aspects of our image export functionality is that generating images is really fast. It takes about 2 seconds for the image generating subprocess to start but once it’s started, generating images can be about as fast as matplotlib and somewhat faster than the selenium-based approach used by other JavaScript visualization libraries like bokeh and altair.

Here are the results of some timing comparisons between plotly.py, matplotlib, bokeh, and altair for saving a 1000 point scatter plot to a PNG and SVG image (get the full notebook here).

Update 09/08/2018: The original version of the chart below showed significantly slower bokeh export times. Thanks to Bryan Van de Ven for pointing out that it is possible to substantially improve the export time by reusing a single selenium webdriver instance across multiple export operations. As far as we know, this approach is not currently available in altair (as of version 2.2.2).