gpt4 book ai didi

python - 获取包含在 Bokeh 中的框选择工具中的选定数据

转载 作者:太空狗 更新时间:2023-10-29 21:33:59 25 4
gpt4 key购买 nike

如果我在 Bokeh 中有散点图并且启用了框选工具,假设我使用框选工具选择了几个点。如何访问我选择的点的 (x,y) 位置信息?

%matplotlib inline
import numpy as np
from random import choice
from string import ascii_lowercase

from bokeh.models.tools import *
from bokeh.plotting import *

output_notebook()


TOOLS="pan,wheel_zoom,reset,hover,poly_select,box_select"
p = figure(title = "My chart", tools=TOOLS)
p.xaxis.axis_label = 'X'
p.yaxis.axis_label = 'Y'

source = ColumnDataSource(
data=dict(
xvals=list(range(0, 10)),
yvals=list(np.random.normal(0, 1, 10)),
letters = [choice(ascii_lowercase) for _ in range(10)]
)
)
p.scatter("xvals", "yvals",source=source,fill_alpha=0.2, size=5)

select_tool = p.select(dict(type=BoxSelectTool))[0]

show(p)

# How can I know which points are contained in the Box Select Tool?

我无法调用“回调”属性,“维度”属性只返回一个列表 ["width", "height"]。如果我能得到选定框的尺寸和位置,我就能从那里找出我的数据集中有哪些点。

最佳答案

您可以在ColumnDataSource 上使用回调,用所选数据的索引更新Python 变量:

%matplotlib inline
import numpy as np
from random import choice
from string import ascii_lowercase

from bokeh.models.tools import *
from bokeh.plotting import *
from bokeh.models import CustomJS



output_notebook()


TOOLS="pan,wheel_zoom,reset,hover,poly_select,box_select"
p = figure(title = "My chart", tools=TOOLS)
p.xaxis.axis_label = 'X'
p.yaxis.axis_label = 'Y'

source = ColumnDataSource(
data=dict(
xvals=list(range(0, 10)),
yvals=list(np.random.normal(0, 1, 10)),
letters = [choice(ascii_lowercase) for _ in range(10)]
)
)
p.scatter("xvals", "yvals",source=source,fill_alpha=0.2, size=5)

select_tool = p.select(dict(type=BoxSelectTool))[0]

source.callback = CustomJS(args=dict(p=p), code="""
var inds = cb_obj.get('selected')['1d'].indices;
var d1 = cb_obj.get('data');
console.log(d1)
var kernel = IPython.notebook.kernel;
IPython.notebook.kernel.execute("inds = " + inds);
"""
)

show(p)

然后您可以使用类似的方式访问所需的数据属性

zip([source.data['xvals'][i] for i in inds],
[source.data['yvals'][i] for i in inds])

关于python - 获取包含在 Bokeh 中的框选择工具中的选定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34164587/

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