如何使用散点图的选定值填充表格?



我正在尝试从散点图中的选择中生成一个表格,bokehinspyder.

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

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

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

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

是否可以获得多个选择的 iDics?

您可以创建一个新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]))

注意:如果您更新到最新的散景版本,则可以使用新的Selection对象。您只需要访问选定的索引,如下所示:new.indices

最新更新