如何通过自定义属性将散点图与Bokeh python库链接



我有两个散点图,它们使用框选择工具,并通过x值链接。我正试图通过一个ID值来连接这些图。对于现有的Bokeh API,是否有一种简单的方法可以做到这一点?

import numpy as np
from bokeh.plotting import figure, output_file, show, gridplot
from bokeh.models import ColumnDataSource
N = 100
max = 100
x = np.random.random(size=N) * max
y1 = np.random.random(size=N) * max
y2 = np.random.random(size=N) * max
id = np.random.random(size=N) * max
output_file("scatter.html")
source = ColumnDataSource(data=dict(x=x, y1=y1, y2=y2))
TOOLS="box_select"
left = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
left.circle("x", "y1", source=source, size=10, fill_color="black", line_color=None)
right = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
right.circle("x", "y2", source=source, size=10, fill_color="black", line_color=None)
p = gridplot([[left, right]])
show(p) 

这两个图并不是"通过x坐标链接"的:它看起来是这样的,因为两个图中的点恰好具有相同的x坐标。如果您为每个数据点分配两个不同的x坐标(x1x2),您会看到它们实际上是通过数据表中的行号链接的(您不需要手动分配id):

import numpy as np
from bokeh.plotting import figure,output_notebook, show, gridplot
from bokeh.models import ColumnDataSource
output_notebook()
N = 100
max = 100
x1 = [0,10,20,30]
x2 = [50,20,10,70]
y1 = [10,10, 20, 20]
y2 = [30,0,30,0]
source = ColumnDataSource(data=dict(x1=x1, x2=x2, y1=y1, y2=y2))
TOOLS="box_select"
left = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
left.circle("x1", "y1", source=source, size=10, fill_color="black", line_color=None)
right = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
right.circle("x2", "y2", source=source, size=10, fill_color="black", line_color=None)
p = gridplot([[left, right]])
show(p)

最新更新