pythonbokehfigure

Is there a way to overlay figures or to get figure handles in bokeh, Python


I created two figures with bokeh and Python which are plotted side by side, then I created another two figures. These are now plotted underneath the first two. What I want is to replace the first two with the new ones. Is there a way to clear the old ones and overlay them with the new ones or at least to get their handles?

Here a simple example

# test.py

from bokeh.layouts import Row
from bokeh.layouts import column
from bokeh.plotting import figure, curdoc

# new plots
s1 = figure(title="my data 1", x_axis_label='x', y_axis_label='y')
s2 = figure(title="my data 2", x_axis_label='x', y_axis_label='y')

# my data
x  = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [3, 5, 7, 9, 11]

# plot lines
s1.line(x, y1, legend="data1", line_width=2)
s2.line(x, y2, legend="data2", line_width=3)
p = Row(s1, s2)

curdoc().add_root(column(p))
print "plot 1 done"

# plot lines again
s1 = figure(title="my data 1", x_axis_label='x', y_axis_label='y')
s2 = figure(title="my data 2", x_axis_label='x', y_axis_label='y')
s1.line(x, y1, legend="data1", line_width=2)
s2.line(x, y2, legend="data2", line_width=3)
p = Row(s1, s2)
curdoc().add_root(column(p))
print "plot 2 done"

output


Solution

  • I found the answer. For this simple example one needs to add

    curdoc().clear()
    

    before the second curdoc().add_root(column(p))

    If you want to do it while keeping buttons or other features alive (because clear deletes everything) here is an example how to deal with it:

    Dynamically add/remove plot using 'bokeh serve' (bokeh 0.12.0)