Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bokeh overlay multiple plot objects in a GridPlot

Tags:

python

bokeh

Say I have a class that holds some data and implements a function that returns a bokeh plot

import bokeh.plotting as bk
class Data():
    def plot(self,**kwargs):
        # do something to retrieve data
        return bk.line(**kwargs)

Now I can instantiate multiple of these Data objects like exps and sets and create individual plots. If bk.hold() is set they'll, end up in one figure (which is basically what I want).

bk.output_notebook()
bk.figure()
bk.hold()
exps.scatter(arg1)
sets.plot(arg2)
bk.show()

enter image description here

Now I want aggregate these plots into a GridPlot() I can do it for the non overlayed single plots

bk.figure()
bk.hold(False)
g=bk.GridPlot(children=[[sets.plot(arg3),sets.plot(arg4)]])
bk.show(g)

enter image description here

but I don't know how I can overlay the scatter plots I had earlier as exps.scatter.

Is there any way to get a reference to the currently active figure like:

rows=[]
exps.scatter(arg1)
sets.plot(arg2)
af = bk.get_reference_to_figure()
rows.append(af) # append the active figure to rows list
bg.figure()     # reset figure

gp = bk.GridPlot(children=[rows])
bk.show(gp)
like image 695
greole Avatar asked Feb 12 '23 14:02

greole


2 Answers

As of Bokeh 0.7 the plotting.py interface has been changed to be more explicit and hopefully this will make things like this simpler and more clear. The basic change is that figure now returns an object, so you can just directly act on those objects without having to wonder what the "currently active" plot is:

p1 = figure(...)
p1.line(...)
p1.circle(...)

p2 = figure(...)
p2.rect(...)

gp = gridplot([p1, p2])
show(gp)

Almost all the previous code should work for now, but hold, curplot etc. are deprecated (and issue deprecation warnings if you run python with deprecation warnings enabled) and will be removed in a future release.

like image 197
bigreddot Avatar answered Feb 16 '23 03:02

bigreddot


Ok apparently bk.curplot() does the trick

exps.scatter(arg1)
sets.plot(arg2)
p1 = bk.curplot()
bg.figure()     # reset figure
exps.scatter(arg3)
sets.plot(arg4)
p2 = bk.curplot()
gp = bk.GridPlot(children=[[p1,p2])
bk.show(gp)
like image 37
greole Avatar answered Feb 16 '23 04:02

greole