24

I need to export pictures of the graphs and plots I am creating with Bokeh.

Usually I do

output_file("test.html")

However, I want to copy that graph into an Excel Sheet. It does not have to be interactive anymore, though that would be brillant. How do I export the graph as a picture? Using code, not clicking on "preview/save".

4
  • 1
    IMHO the html is the only (file) option at the bokeh session management, you can use this to convert html to jpg.
    – Kobi K
    Jun 5, 2014 at 12:42
  • I don't have permissions to install PyQt. Jun 5, 2014 at 12:48
  • Try this post.
    – Kobi K
    Jun 5, 2014 at 12:51
  • Seems to me like it is only for text. Jun 5, 2014 at 13:31

2 Answers 2

15

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.

2
  • the export_png function increases my running time about 15 seconds, do you experience the same?
    – user8678484
    Jun 4, 2018 at 12:21
  • Yes, I experience this too
    – Temak
    Sep 25, 2018 at 21:48
0

Alternatively, if you are willing to work with JavaScript. And, for instance, if you want to save many canvas (each canvas element has a plot) at the same time you can use the JavaScript method canvas.toDataUrl() to convert the canvas to png as base64. When you get all the images you can do whatever you want with them. These images have 96dpi and it cannot be changed, so if you want more resolution you will have to update the sizes of all the elements of the plot before the convertion as well: fonts, axis, plot size...

If you use this approach you do not need to install selenium and phantomjs dependencies in your python environment.

Also, be aware that if you use export_png and you export the plot with a bigger size, the axis and fonts are not going to be proportionally bigger

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.