问题
I was hoping some heavy Jupyter and bokeh users might have a workaround for this. So I have a bunch of bokeh
plots in my Jupyter notebook and I want to export the notebook to pdf
. In the Jupyter Notebook file menu there is an option to download a notebook as a pdf
. Also--and my preferred route--is to use nbconvert.
The problem is that the bokeh
plots are not exported into the final pdf
file. Bokeh
does not export its plots to a format that is caught by Latex
to compile the pdf. In constrast, a package like matplotlib
will generate png files for each plot, and then those plots are linked in the latex to generate the pdf.
Right now when I export the notebook, all of the text comes out in the pdf but none of the plots come through. The plots come in if I export to an html file--but of course the plots spill off the page if I try and print the document.
Has anyone found a decent workaround for this? The users I work with still want to be able to print out documents and mark them up.
回答1:
Standard interactive Bokeh plots are actually collections of JSON, together with a JavaScript library (BokehJS) that renders the JSON as the desired plot in a browser. Since PDF documents do not execute JavaScript, it will never be possible to embed standard Bokeh plots in PDF form.
However, Bokeh can also export static versions of plots as PNG or SVG, which can be embedded in PDFs. See the section Exporting Plots in the User's guide. You will first need to install some optional dependencies (phantomJS, selenium and pillow) and then to export the plot will be something like this:
export_png(plot, filename="plot.png")
Then, specifically in the notebook, you can display images inline with something like:
from IPython.display import Image
Image('plot.png')
来源:https://stackoverflow.com/questions/48231910/jupyter-and-bokeh-workaround-for-exporting-bokeh-plots-when-exporting-jupyter-n