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

标签 python bokeh

我有两个使用框选择工具的散点图,并由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)

关于python - 如何通过自定义属性将散点图与 Bokeh python 库链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138496/

相关文章:

javascript - Bokeh python ;使用 ColumnDataSource 上的回调来更改带有 Select Widget 的堆叠条形图

python - 使用 Pandas 在 MySQL 中创建一个临时表

python-3.x - 在 Bokeh python 中创建雷达图的步骤是什么?

python - 在 Bokeh 服务应用程序中绘制本地镜像

python - NLTK 正则表达式分块器不在一个命令中处理多个语法规则

python - 在 Bokeh 中创建选择列表时出错

python - 自版本 2.0.0 以来 Bokeh 图损坏

python - 列表理解和生成器表达式中的 yield

Python:在一系列脚本之间共享通用代码

python - Python 如何处理全局变量?