代码之家  ›  专栏  ›  技术社区  ›  MRocklin

如何从Bokeh图中清除所选内容?

  •  1
  • MRocklin  · 技术社区  · 6 年前

    我使用图中的选择来驱动我的Bokeh服务器应用程序。但是,在用户选择了一些我实际上不希望该选择对图形有任何视觉效果的东西之后。如何删除选择效果?

    我可以想象有两种方法可以解决这一问题,但我很难找到其中一种方法:

    1. 在回调中删除所选内容

      def cb(attr, old, new):
          source.selected.indices.clear()
          ...
      
      source.on_change('selected', cb)
      
    2. 保留所选索引,但删除它们之间的任何样式差异

      我找到了 http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs 但不知道如何有效地将其应用于我的问题。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Mateusz Paprocki    6 年前

    选择/非选择glyph可以禁用或使用主glyph,例如:

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

    r = plot.scatter(...)
    r.selection_glyph = r.glyph
    r.nonselection_glyph = r.glyph
    
        2
  •  1
  •   ChesuCR Ayansplatt    6 年前

    方案1

    我已经写了一篇 GH issue 来解决这个错误。我们应该有可能以编程方式更新所选样本。

    选项2

    如果你只想隐藏选择索引,你可以按照Mateusz说的做。

    通常情况下,您只有一个带有选中和未选中元素的glyph,如下所示:

    c = self.plot.scatter(
        x='x',
        y='x',
        size=4,
        line_color=None,
        fill_color='blue',
        source=source,
        view=view,
    
        nonselection_line_color=None,
        nonselection_fill_color='blue',
        nonselection_fill_alpha=1.0,
    )
    c.selection_glyph = Circle(
        line_color=None,
        line_alpha=1.0,
        fill_color='red',
    )
    

    但是,如果要更改选定内容,并将选定内容的颜色保留在自定义选定内容上,作为解决方法,您可以管理其他列表 custom_selection 实际选择的样本。因此,您需要创建两个glyph,一个用于选定的,另一个用于未选定的样本。像这样:

    c = self.plot.scatter(
        x='x',
        y='x',
        size=4,
        line_color=None,
        fill_color='blue',
        source=source,
        view=view_non_selected,          # here the view should have the non-selected samples
    
        nonselection_line_color=None,
        nonselection_fill_color='blue',
        nonselection_fill_alpha=1.0,
    )
    c.selection_glyph = Circle(
        line_color=None,
        line_alpha=1.0,
        fill_color='blue',  # I plot the selected point with blue color here as well
    )
    
    c_sel = self.plot.scatter(
        x='x',
        y='x',
        size=4,
        line_color=None,
        fill_color='red',
        source=source,
        view=view_selected,          # here the view should have the selected samples
    
        nonselection_line_color=None,
        nonselection_fill_color='red',
        nonselection_fill_alpha=1.0,
    )
    c_sel.selection_glyph = Circle(
        line_color=None,
        line_alpha=1.0,
        fill_color='red',  # I plot the selected point with blue color here as well
    )
    

    每次更新所选内容时,都必须更新视图索引列表:

    view_non_selected.filters = [IndexFilter(non_selected_indices_list)]
    view_selected.filters = [IndexFilter(custom_selection)]
    

    您也可以创建一个带有颜色列的单个glyph,并更新源代码。它可能更有效。

    但如果我是你,我会等待修复错误。