Bokeh – Exporting Plots

Bokeh - Exporting Plots

In this chapter, we will discuss Bokeh Exporting Plots. For that purpose, local Python installation should have the following dependency libraries.

Bokeh Exporting Plots purpose

PhantomJS

PhantomJS is a JavaScript API that enables automated navigation, screenshots, user behavior, and assertions. It is PhantomJS is based on WebKit providing a similar browsing environment for different browsers and provides fast and native support for various web standards: DOM handling, CSS selector, In other words, PhantomJS is a web browser without a graphical user interface.

Pillow

Pillow, a Python Imaging Library (earlier known as PIL) is a free library for the Python programming language that provides support for opening, manipulating, and saving many different image file formats. (including PPM, PNG, JPEG, GIF, TIFF, and BMP.) Some of its features are per-pixel manipulations, masking and transparency handling, image filtering, image enhancing, etc.

The export_png() function generates RGBA-format PNG image from layout. This function uses Webkit headless browser to render the layout in memory and then capture a screenshot. The generated image will be of the same dimensions as the source layout. Make sure that the Plot.background_fill_color and Plot.border_fill_color are properties to None.

from bokeh.io import export_png
export_png(plot, filename = "file.png")

It is possible that HTML5 Canvas plots output with an SVG element that can be edited using programs such as Adobe Illustrator. Like PNGs, in order to create an SVG with a transparent background, the Plot.background_fill_color and Plot.border_fill_color properties should be to None.

The SVG backend is first activated by setting the Plot.output_backend attribute to “SVG”.

plot.output_backend = "svg"

For headless export, Bokeh has a utility function, export_svgs(). This function will download all SVG-enabled plots within a layout as distinct SVG files.

from bokeh.io import export_svgs
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")

Next Topic – Click Here

This Post Has 2 Comments

  1. superslot789

    Major thankies for the blog. Awesome.

Leave a Reply