简体   繁体   中英

Changes to Bokeh plot (e.g. zooming) not reflected when calling export_png

I have a Bokeh plot in a Jupyter notebook that can be zoomed and panned. In a later notebook cell I would like to call Bokeh's export_png function to export the plot exactly as it is displayed, however, the exported plot doesn't reflect the current zoom status of the plot.

Eg

from bokeh.plotting import figure, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# create a new plot with a title and axis labels
p = figure(title="Simple line example", x_axis_label="x", y_axis_label="y")

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

# show the results
show(p)

And then in later cell:

from bokeh.io import export_png

export_png(p, filename="plot_test.png")

I could click the "save" button in the plot toolbar, this saves the plot exactly as it is displayed in the notebook with the current zoom state, however I'm curious if it would be possible to do this programmatically in the notebook?

This is definitely not possible with basic standalone output (ie output_notebook and show on a plot object) because once the plot is shown, the browser/JS side of things is completely independent and untethered to anything on the "Python side". The only ways this might conceivably be possible are:

  • Adding a SaveTool to the plot and clicking the toolbar button for it — this generates a PNG from JavaScript using he current state on the "JS side"

  • Embed a Bokeh server application instead. The Bokeh server is precisely the thing that exists for keeping the "Python side" and "JS side" synchronized. But you would only be able to call export_png from a callback that is set up, eg on a Button click or tap or any of the supported events. It would not work from external notebook cells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM