Exporting figures from Bokeh as svg or pdf?

Question:

Is it possible to output individual figures from Bokeh as pdf or svg images? I feel like I’m missing something obvious, but I’ve checked the online help pages and gone through the bokeh.objects api and haven’t found anything…

Asked By: surfreak

||

Answers:

It seems that since bokeh uses html5 canvas as a backend, it will be writing things to static html pages. You could always export the html to pdf later.

Answered By: jjwon

In the meantime… as a workaround, until we get a native support, you can use phantom.js to convert the HTML output into a pdf file. We use it in our example testing directory to convert HTML generated plots into png images, but you could also get pdf images:

And more info here:

Answered By: Damian Avila

There is no way to save PDF currently, but as of Bokeh 0.12.6, it is now possible to export PNG and SVG directly from
Python code.

Exporting PNGs looks like this

export_png(plot, filename="plot.png")

And exporting SVGs looks like this

plot.output_backend = "svg"
export_svgs(plot, filename="plot.svg")

There are some optional dependencies that need to be installed.
You can find more information in the Exporting Plots section of the User Guide.

Answered By: Optional Argument

So, it’s 2022 now and there’s no direct support for exporting images as pdf.
However, there is a alternative way which works just as well.

First, as the other answers have mentioned, set the backend to ‘svg’.
plot.output_backend = "svg"
then save the image as an svg file. Do not save it as an html file as this will likely lead to extra space around the actual image.
Then use any online svg to pdf convertor to get the pdf equivalent of the svg.

Answered By: simpleParadox
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.