可点击的字形在散景上生成图

问题描述

使用散景,我想生成带有可以选择或不选择的字形的图形。给定字形的选择应导致使用与该给定字形点相关联的特定数据更新绘图。我希望能够同时单击并选择多个字形,并且该图显示所有不同的数据。

下面的代码显示了我的位置

from bokeh.io import show,curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
import pandas as pd
import numpy as np

# Create data for clickable plot
x = [0,1]
y = [0,1]
table_index = [0,1]

# Create the clickable plot
plot = figure(height=400,width=600,title='Select a point',tools='tap')
plot_source = ColumnDataSource(data=dict(x=x,y=y))
renderer = plot.circle('x','y',source=plot_source,size=30)

# Create two sets of data for the updatable plot
master_data = {}

master_data[0] = {'x_values': [1,2,3,4,5],'y_values': [6,7,6]}
master_data[1] = {'x_values': [1,5,2]}

# Create updatable plot
data = master_data[0]
plot_source2 = ColumnDataSource(data=data)
c = figure(title="test",x_axis_label='numbers',y_axis_label='more numbers')
c.line(x='x_values',y='y_values',source=plot_source2,line_width=2)

# Here the reactions of the server are defined
def my_tap_handler(attr,old,new):
    index = new[0]
    c.line.source = ColumnDataSource(master_data[index])  #problematic line

plot_source.selected.on_change("indices",my_tap_handler)

# Collect it all together in the current doc
curdoc().add_root(column(plot,c))
curdoc().title = 'Select experiment'

此代码运行并生成一个可点击的图以及带有数据的图,但是它有两个问题:首先,我无法在“可点击的图”上选择两个(或都不选择!)字形,以便在“可更新绘图”中显示两个数据集(或不显示任何数据!)。其次,上面有问题的一行没有做应做的事情。

也许我不想使用on_tap处理程序,而不想使用on_click,但是我不知道如何使用。顺便说一句,我想直接在jupyter笔记本上运行它,但是现在我被迫保存一个常规的python代码“ test.py”并通过bokeh serve --show test.py运行它。有没有办法将所有这些内容都嵌入Jupyter?

解决方法

有多种方法:

  • 切换到multi_line并为其更改数据(如下所示)
  • 切换到multi_line并使用基于索引的过滤器创建“ CDSView”
  • 继续使用line,但在开始时先绘制所有内容,然后再更改其visible属性

它并没有真正被记录在任何地方(至少,我什么都找不到),但是用水龙头选择字形也要注意Shift和Ctrl键,尽管这不是很直观。这是Bokeh的相关代码,应该不言自明。

  protected _select_mode(ev: UIEvent): SelectionMode {
    const {shiftKey,ctrlKey} = ev

    if (!shiftKey && !ctrlKey)
      return "replace"
    else if (shiftKey && !ctrlKey)
      return "append"
    else if (!shiftKey && ctrlKey)
      return "intersect"
    else if (shiftKey && ctrlKey)
      return "subtract"
    else
      unreachable()
  }

还请注意,没有选择并且选择了所有字形在外观上没有区别,但是回调将收到不同的值。

这是您要实现的目标的示例实现。

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure

plot = figure(height=400,width=600,title='Select a point',tools='tap')
plot_source = ColumnDataSource(data=dict(x=[0,1],y=[0,1]))
renderer = plot.circle('x','y',source=plot_source,size=30)

master_data = [{'x_values': [1,2,3,4,5],'y_values': [6,7,6]},{'x_values': [1,5,2]}]

plot_source2 = ColumnDataSource(data=dict(x_values_s=[],y_values_s=[]))
c = figure(title="test",x_axis_label='numbers',y_axis_label='more numbers')
c.multi_line(xs='x_values_s',ys='y_values_s',source=plot_source2,line_width=2)


def my_tap_handler(attr,old,new):
    print(new)
    plot_source2.data = dict(x_values_s=[master_data[i]['x_values'] for i in new],y_values_s=[master_data[i]['y_values'] for i in new])


plot_source.selected.on_change("indices",my_tap_handler)

curdoc().add_root(column(plot,c))
curdoc().title = 'Select experiment'

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...