为了说明我的问题,我改编了here的示例。
假设我有几个不同维度的数据集,我想在同一图中绘制然后进行选择,我如何将所选数据存储在例如字典(或任何其他结构如numpy数组,pandas数据帧,...)?所以数据选择看起来像这样
现在有办法以表格形式存储所选数据:
{'x': ['x_1_selected', 'x_2_selected', '...', 'x_n_selected'],
'x1': ['x1_1_selected', 'x1_2_selected', '...', 'x1_n_selected'],
'y': ['y_1_selected', 'y_2_selected', '...', 'y_n_selected'],
'y1': ['y1_1_selected', 'y1_2_selected', '...', 'y1_n_selected']}
给出一些背景知识: 应该允许用户加载不同的数据集,可视化它们,使用套索进行选择,然后选择的数据应该用于其他函数,因此它们需要存储在一个结构中,该结构可以很容易地传递给我定义的其他函数
是否有,例如一种全球更新s2
和s2b
的方式(请参阅下面的代码)?当我现在检查s2.data
和s2b.data
时,他们仍会显示空列表。
这是代码:
from random import random
from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show
output_file("callback.html")
x = [random() for x in range(500)]
y = [random() for y in range(500)]
x2 = [random() for x2 in range(100)]
y2 = [random() for y2 in range(100)]
# the two different data sources of different length
s1 = ColumnDataSource(data=dict(x=x, y=y))
s1b = ColumnDataSource(data=dict(x2=x2, y2=y2))
# the figure with all source data where we make selections
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6, color='red')
p1.circle('x2', 'y2', source=s1b, alpha=0.6, color='black')
# second figure which is empty initially where we show the selected datapoints
s2 = ColumnDataSource(data=dict(x=[], y=[]))
s2b = ColumnDataSource(data=dict(x2=[], y2=[]))
p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1),
tools="", title="Watch Here")
p2.circle('x', 'y', source=s2, alpha=0.6, color='red')
p2.circle('x2', 'y2', source=s2b, alpha=0.6, color='black')
# individual callback for different datasets
s1.callback = CustomJS(args=dict(s2=s2), code="""
var inds = cb_obj.selected['1d'].indices;
var d1 = cb_obj.data;
var d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
""")
s1b.callback = CustomJS(args=dict(s2b=s2b), code="""
var inds = cb_obj.selected['1d'].indices;
var d1 = cb_obj.data;
var d2 = s2b.data;
d2['x2'] = []
d2['y2'] = []
for (i = 0; i < inds.length; i++) {
d2['x2'].push(d1['x2'][inds[i]])
d2['y2'].push(d1['y2'][inds[i]])
}
s2b.change.emit();
""")
layout = row(p1, p2)
show(layout)
答案 0 :(得分:1)
鉴于您提到将所选数据存储在numpy数组或pandas数据帧中,我假设您计划实际使用b[1:2]
。在这种情况下,您不需要编写任何JS代码,因为所有Bokeh [[3,4]]
都具有bokeh serve
属性,并且您可以附加Python回调。只需用以下代码替换回调代码:
DataSource