gpt4 book ai didi

python - Bokeh ,套索选择表更新

转载 作者:太空宇宙 更新时间:2023-11-04 00:21:28 25 4
gpt4 key购买 nike

希望大家一切顺利

我正在尝试开发一个 Bokeh 交互,通过选择散点图的一部分来更新表格。

我使用了 Bokeh 文档中的大量示例代码。我的工作场所运行的是旧版本的 Bokeh (0.12.5),所以我不得不更改自定义 JS 中的最后一行(从 s2.change.emit() 到 s2.trigger('change))。然后我添加了一些行来创建数据表。

我天真地认为,既然在数据表中获取“s1”有效,那么获取“s2”将允许我将表链接到套索选择。我什至尝试在 JS 回调中向表小部件添加一个额外的触发器。

有谁知道如何从图表中的套索选择创建表格?

代码

提前致谢。

from random import random

from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

output_file("callback.html")

x = [random() for x in range(500)]
y = [random() for y in range(500)]

s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6)

s2 = ColumnDataSource(data=dict(x=[], y=[]))
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)

###New code##
columns = [TableColumn(field ="x", title = "X axis"),
TableColumn(field ="y", title = "Y axis")]

table = DataTable(source =s2, columns = columns, width =400, height = 280)


##Added in table.trigger('change') hoping this would link to the lasso select.
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.trigger('change');
table.trigger('change');
""")


##having 'table' in the layout, stops the callback from working, deleting table from the layout makes it work.
layout = row(p1, p2, table)

show(layout)

最佳答案

这是 bokeh 1.0.4 的工作版本

from random import random

import bokeh.io
from bokeh.io import output_notebook, show

from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

from bokeh.resources import INLINE
bokeh.io.output_notebook(INLINE)

x = [random() for x in range(500)]
y = [random() for y in range(500)]

s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6)

s2 = ColumnDataSource(data=dict(x=[], y=[]))
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)

columns = [TableColumn(field ="x", title = "X axis"),
TableColumn(field ="y", title = "Y axis")]

table = DataTable(source =s2, columns = columns, width =155, height = 380)


s1.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2, table=table), code="""
var inds = cb_obj.indices;
var d1 = s1.data;
var d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (var i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
table.change.emit();
""")
)

layout = row(p1, p2, table)

show(layout)

您甚至可以在表格中选择行/值,第二个绘图中的点将改变不透明度(您可以通过单击标题对表格进行排序)

working table callback in bokeh, screen shot

关于python - Bokeh ,套索选择表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48982260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com