如果我在散景中有一个散点图并且我启用了Box Select Tool,假设我使用Box Select Tool选择了几个点.如何访问我选择的点的(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?
我不能调用"callback"属性,而"dimensions"属性只返回一个列表["width","height"].如果我可以获得选定框的尺寸和位置,我可以从那里找出我的数据集中的哪些点.
您可以使用callback
上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])