Search code examples
pythonpython-3.xplotbokeh

How to clear a selection from a Bokeh plot?


I use selection in a figure to drive my Bokeh server application. However after the user selects something I don't actually want that selection to have any visual effect on the figure. How can I remove the selection effects?

I can imagine two ways of solving this, but am having trouble getting either to work:

  1. Remove the selection in the callback

    def cb(attr, old, new):
        source.selected.indices.clear()
        ...
    
    source.on_change('selected', cb)
    
  2. Keep the selected indices, but remove any styling difference between them. I found this:

    http://docs.bokeh.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs

    but wasn't sure how to effectively apply this to my problem.


Solution

  • Selection/non-selection glyphs can be either disabled or use the main glyph, e.g.:

    r = plot.scatter(...)
    r.selection_glyph = None
    r.nonselection_glyph = None
    

    or

    r = plot.scatter(...)
    r.selection_glyph = r.glyph
    r.nonselection_glyph = r.glyph