【问题标题】:How to fill a table with the selected values of a Scatter Plot?如何用散点图的选定值填充表格?
【发布时间】:2023-03-30 12:27:01
【问题描述】:

我正在尝试根据bokehspyder 中的散点图中的选择生成一个表格。

使用ColumnDataSource我可以刷链接不同的情节。

我想用选定的值填充表格。

我通过回调成功获取了单个选择的索引。

def callback(attr, old, new):
    patch_name =  source.data[new['1d']['indices'][0]]
    print(patch_name)

是否可以得到多选的索引?

【问题讨论】:

    标签: python python-3.x plot callback bokeh


    【解决方案1】:

    您可以创建一个新的ColumnDataSource 来构建DataTable 并在每次进行选择时更新此数据表的数据:

    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure
    from bokeh.models.widgets.tables import DataTable, TableColumn
    from bokeh.layouts import row
    from bokeh.io import curdoc
    
    source = ColumnDataSource(dict(
        x=[1, 2, 3, 4, 5, 6],
        y=[1, 2, 3, 4, 5, 6],
    ))
    
    p = figure(
        plot_height=300,
        tools='lasso_select'
    )
    
    rc = p.scatter(
        x='x',
        y='y',
        size=20,
        color='red',
        source=source,
        fill_alpha=1.0,
        line_alpha=1.0,
    )
    
    columns = [
        TableColumn(field="value", title="Value"),
    ]
    init_cds = ColumnDataSource(data=dict(value=['']))
    table = DataTable(
        source=init_cds,
        columns=columns,
        reorderable=False,
    )
    
    def update_table(attr, old, new):
        print(new.indices)
        if new.indices != []:
            new_vals_dict = {'value': new.indices}
        else:
            new_vals_dict = {'value': ['']}
        table.source.data = new_vals_dict
    
    source.on_change('selected', update_table)
    
    curdoc().add_root(row(children=[p, table]))
    

    注意:如果您更新到最新的 Bokeh 版本,您可以使用新的 Selection 对象。您只需要像这样访问选定的索引:new.indices

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多