File output in Bokeh

Table of Content

Updated on September 23, 2020:
There seems a bug in bokeh 2.2.1, in which png files do not contain whole of the graphic area when exporting to png from html. To avoid this error, use bokeh 1.4.0, which can be insalled with conda install -c pyviz bokeh==1.4.0 in a new conda virtual environment

This article is targeted for Windows 10. Although Matplotlib is a conventional plotting tool in Python, Bokeh is a relatively new package that makes it easy to create interactive plots. Unfortunately, the image output for pasting into PowerPoint or Word is low resolution by default, and it is necessary to enhance the resolution by setting the value of vertical and horizontal pixels to be large at the stage of creating graph. However, since the size of the character does not change in this way, it is very troublesome to set the size of the character each time the pixel values are changed. I found an easy solution for this issue as introduced below.

Output in html

Bokeh makes it easy to output html and the toolbar works like in Jupyter Notebook, so it can be used interactively. First, import save as follows:

from bokeh.io import save

Suppose graphic object is p, its html file can be saved quickly as follows:

save(p, filename='output.html')

By double clicking on the created output.html and displaying it in the browser (e.g., Google Chrome), it can also be run interactively. PNG file output is also possible using an interactive function of the toolbar in the graph. In addition, you can increase the resolution by enlarging the graph with the zoom function of the browser and outputting the PNG file from the toolbar. Characters are also enlarged according to the zoom. As this html file is like in a vector format, characters are not jagged when it is enlarged.

In conclusion, it is recommended that save as html file, display it in browser, zoom in appropriately, and save as PNG file.
Although it is also possible to save as PNG file by export_png (p, 'output.png'), it is very slow.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.