Violet
Violet

Reputation: 361

Generate inline Bokeh scatterplots in Jupyter using a for loop

I am trying to plot three Bokeh scatterplots inline in a Jupyter notebook. I’ve been able to do this previously, but after updating Bokeh, I can't get it to work. Is there something I need to do differently now?

from bokeh.plotting import figure, output_file, output_notebook, show
output_notebook()


x = dfList[0][1]['ValueA']

for i in range(0,3):

    #Define figure
    p = figure(plot_width=900, plot_height=600,     
          tools="pan,wheel_zoom,lasso_select,box_zoom,reset,save,undo")

    #Add four datasets to figure
    for t in range(4):
        y = dfList[t][1]['ValueB']
        plot_data = p.circle(x,y)

    #Display figure in notebook
    show(p)

With the code above, only the first plot displays. I have tried

from bokeh.plotting import reset_output

and adding reset_output() after show(p) on each iteration, which does generate the three plots, but they are each output in a separate browser tab, which is not what I want.

If I also add output_notebook() each iteration, rather than just in the first cell of my notebook, then I still only get the first plot, with the “Loading BokehJS…” message displayed below for the other two iterations.

What am I doing wrong?

Upvotes: 0

Views: 1315

Answers (1)

bigreddot
bigreddot

Reputation: 34568

If that worked previously I'd say it was unintentional undefined behavior. The show function has always had in mind that it was for replacing the output in the next output cells, so running it multiple times in one cell was never a usage pattern that was considered. In any event, adding support for support JupyterLab has necessitated various changes to notebook display machinery, and that's probably the immediate reason you are seeing a difference. However, I would say that the current behavior is correct, and what should be expected going forward.

The right way to do something like this is to collect the plots in a layout of some kind, and then show the layout:

from bokeh.layouts import column
layout = column()

for x in foo:

    p = figure()
    layout.children.append(p)

show(layout)

Upvotes: 3

Related Questions