我正在尝试使用回调获取散景散布图上套索选定点的数据。
我正在研究此处显示的示例:Bokeh Server callback from tools
from bokeh.plotting import figure, curdoc, show, output_file
from bokeh.models import ColumnDataSource
from bokeh.layouts import column
from bokeh.io import curdoc
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('XY'))
source=ColumnDataSource(df)
p = figure(title="Some Figure", tools=["lasso_select"])
pglyph = p.circle(x='X', y='Y', source=source)
def callback(attr, old, new):
# The index of the selected glyph is : new['1d']['indices'][0]
patch_name = source.data['X'][new['1d']['indices'][0]]
print("LassoTool callback executed on Patch {}".format(patch_name))
pglyph.data_source.on_change('selected',callback)
curdoc().add_root(column(p))
#bokeh serve --show TestApp.py
使用Bokeh Server运行此脚本时,需要做哪些更改才能使打印功能正常工作?这将帮助我了解如何访问所选功能的数据以用作其他图表的源。
答案 0 :(得分:1)
该链接代码已经过时。使用任何相对较新版本的Bokeh进行此操作的正确方法是:
def callback(attr, old, new):
patch_name = source.data['X'][new]
print("LassoTool callback executed on Patch {}".format(patch_name))
pglyph.data_source.selected.on_change('indices',callback)