Skip to content Skip to sidebar Skip to footer

Interactively Change Glyphs In Bokeh Plot

I'm trying to generate a bokeh application which allows the user to change the glyphs of a plot. Unfortunately, the glyphs don't change after calling on_change() method of the drop

Solution 1:

I'm not sure about the exact reasons why your approach doesn't work, but this one works:

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Dropdown
from bokeh.plotting import figure

source = ColumnDataSource(dict(x=[1, 2, 3], y=[4, 5, 6]))

plot = figure()

renderers = {rn: getattr(plot, rn)(x='x', y='y', source=source,
                                   **extra, visible=False)
             for rn, extra in [('circle', dict(size=10)),
                               ('line', dict()),
                               ('cross', dict(size=10)),
                               ('triangle', dict(size=15))]}


deflabel_fn(item):
    return'Select marker ({})'.format(item)


menu = [('No renderer', None)]
menu.extend((rn.capitalize(), rn) for rn in renderers)

dropdown = Dropdown(label=label_fn(None), menu=menu, value=None)


defupdate_plot(attr, old, new):
    dropdown.label = label_fn(new)
    for renderer_name, renderer in renderers.items():
        renderer.visible = (renderer_name == new)


dropdown.on_change('value', update_plot)

lay_out = layout([plot, dropdown])

curdoc().add_root(lay_out)

Basically, I create all necessary renderers beforehand, and then just switch visible flag of each one.

Also, note the correct terminology. What you're calling a plot, is not actually a plot but a glyph renderer. And plot and figure are basically the same thing.

Post a Comment for "Interactively Change Glyphs In Bokeh Plot"