How to save bokeh plots in non html formats [duplicate] - python

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".

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.

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

Related

How to export plotly plots, tabs and images into a single html

I would like to know if there is a way to export multiple plotly plots, tabs and images into a single HTML file.
In detail the problem is this. I want to create a standalone .exe file with python able to plot several graphs and images, given some input files containing the data; writing code with PySimpleGUI for the GUI.
But once the several interactive graphs, tabs and images are plotted in a window, I would like to add in the window two buttons to export these data respectively in a excel file (so with no interactivity) and in a HTML file (to keep interactivity).
The first point is no trouble for me: I use xlsxwriter to export images and graphs as images. The problem is the second point: I would like to export all these data keeping interactions in graphs in a single HTML with a certain design I choose. Remember the fact that I want to build a standalone .exe file, because this program will run in a PC with Python not installed.
My question is this: is there some python library or html writer in python I can use to do this?
P.S.: plotly graphs are not subplots of a single plots but each one is a single plot, each one will have its own zoom etc. commands.

python GUI: how to create a "table" to incude both chart (by matplotlib) and text (pandas data)?

I have several groups of data and I want to have a table to both visualize them and summarize some important features. What I want is something like the following:
The data are known and it is easy to get these features. But how to have a table (or whatever widget, as long as it shows like a table) to include both the small chart like a thumbnail (plotted by matplotlib)?
Is there a way I can use any Python GUI library (e.g., tkinter) to make it? I would appreciate any help! Thank you!
This image is created in Excel :(
We should be able to control the size of the chart so that it can "fit" the font of the text (data).
The borderlines are preferred but not required.
This is something you could use pyside/pyqt for. In pyqt all text labels can also be set as images, so you can just save your images from matplotlib and load them in PYQT. PYQT has an editor that generates your front end code, so I would recommend just downloading it and playing a bit with the editor. But PYQT is only good if you want a live GUI.
If you want to generate a file, just use excel like you are already doing with openpyxl (https://openpyxl.readthedocs.io/en/stable/)? I use it for a lot of my reports. With PIL you can add images from matplotlib.

Saving images from plotly

How can I save images generated with plotly in different formats? Only "Download as PNG" is possible from the generated HTML figure. I would need to interact with the figure (change rotation, choose which data to plot) and save an .eps figure for each online modified plot. Thanks a lot!
Plotly supports exporting to EPS (the docs mention that you need the poppler library) and the Figure object has a write_image method that saves a figure to a file.
You can specify the format through the filename and the resolution with the width and height keyword arguments, representing logical pixels.
You can read more on static image exporting in Plotly here. This is a code example:
fig.write_image("name.eps", width=1920, height=1080)
In order to select what is plotted you will have to set the figure's camera controls.

Bokeh change quality export plot

I am trying to export some grid into either png or svg but with svg option—as the doc states-the plots are exported as multiple files. So the only option is png. (For some reason if I use the toolbar, I only get the first plot and not the other).
But the quality of the png is terrible. Is there an option to set dpi or whatever? Even I you try to save graphs on the official documentation, you obtain poor quality.
I really like Bokeh and found it nicer to use that any other libs but this quality export is critical for me (publication purpose).
In recent versions of Bokeh the export_png accepts width and height parameters that you can set as arbitrarily large as you like, e.g:
export_png(p, "plot.png", width=2400, height=1000)

With Bokeh, how to save to a png or jpg instead of a html file?

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".
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.
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

Categories