Can we specify bokeh exported png file size / resolution?
Asked Answered
P

2

3

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.

Prohibition answered 20/12, 2018 at 0:32 Comment(2)
You can use code tags to format code, try surrounding the commands with backticks (`)Reenareenforce
Sorry, I don't quite understand your comment. I am using Bokeh in Python.Prohibition
G
0

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.

Gresham answered 26/9, 2019 at 11:21 Comment(0)
U
2

In recent enough versions, export_png accepts width and height parameters:

https://docs.bokeh.org/en/latest/docs/reference/io.html#bokeh.io.export_png

If they are not available in your current install then you will need to update your Bokeh installation to something newer.

Ulick answered 20/12, 2018 at 1:0 Comment(1)
Thanks for your help. In my current version 0.12.16, we have the width and height parameters. However, they only work for Plot object, the same as export_svgs(). Ideally, the solution can work for all objects, including column() and row()Prohibition
G
0

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.

Gresham answered 26/9, 2019 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.