Search code examples
pythonexportpngbokeh

Can we specify bokeh exported png file size / resolution?


With Python Bokeh export_png function, we can export an object to png file. However, I cannot find any way to specify the resulted png file size / resolution.

I found that I used the same code and got different png files in Windows and Linux. In Windows, the resolution is much lower.

I understand we can use export_svgs() to have better figure file. But compared with export_png(), export_svgs() has many limitations. We cannot use export_svgs() on div, column or row objects.


Solution

  • There is a way to increase the resolution when the plot is exported with export_png (which @bigreddot says in his answer), that is setting width and height directly in the method:

    export_png(p2, filename="plot.png", height=300, width=300)
    

    plot_300

    The problem is that the axis and font size keep always the same size, no matter how big the plot is:

    export_png(p2, filename="plot.png", height=1200, width=1200)
    

    plot_1200

    The possible solution right now is to update the axis attributes manually. I think that an easy fix would be to increase, in proportion, the size of the axis and fonts before exporting.

    Also, 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 the last approach you do not need to install selenium and phantomjs dependencies

    Anyway if you wait for the 2.0 version you will get this for gridplots:

    At bokeh 2.0, gridplot() will produce an instance of a single canvas GridPlot model. The old behavior will still be possible with an existing GridBox layout.