【问题标题】:Bokeh, lasso select to table update散景,套索选择表更新
【发布时间】:2018-08-05 12:58:54
【问题描述】:

我希望每个人都做得很好。

我正在尝试开发散景交互,从而选择散点图的一部分将更新表格。

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

我天真地认为,由于在 Datatable 中采购 '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)

【问题讨论】:

    标签: python bokeh


    【解决方案1】:

    这是 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)
    

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

    【讨论】:

      【解决方案2】:

      现在,您的回调不知道“table”是什么。您需要将其作为参数传递给您的 CustomJS 函数:

      s1.callback = CustomJS(args=dict(s2=s2, table=table), 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');
      """)
      

      【讨论】:

      • 嗨。感谢您对这个 Saguara 的帮助。非常感谢。
      • 我的代码仍然存在问题。也许这是我在工作中使用的 Python 版本。今晚我将尝试在我的家用笔记本电脑上运行代码。干杯
      猜你喜欢
      • 2018-11-09
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多