How do I retrieve information about the selected data using the box select function in the bokeh?
from bokeh.layouts import gridplot
from bokeh.io import output_notebook, show
from bokeh.plotting import figure, output_file
from bokeh.models import ColumnDataSource
output_notebook()
x = list(range(-20,21))
y0, y1 = [abs(xx) for xx in x], [xx**2 for xx in x]
# create a column data source for the lots to share
source=ColumnDataSource(data=dict(x=x,y0=y0,y1=y1))
TOOLS="box_select, lasso_select, help"
# create a new plot and add a render
left=figure(tools=TOOLS, width=300, height=300)
left.circle('x', 'y0', source=source)
# create another new plot and add a render
right=figure(tools=TOOLS, width=300, height=300)
right.circle('x', 'y1', source=source)
p=gridplot([left, right]])
show(p)
The bokeh.models.CustomJS
class allows JavaScript to retrieve information about selected objects.
p=gridplot([left, right]])
from bokeh.models import CustomJS
source.selected.js_on_change('indices', CustomJS(args=dict(s=source), code="""
constinds = s.selected.indices;
constd = s.data;
console.log(inds);
"""))
show(p)
For example, if you select two points on a graph, the index of the selected point is printed to the browser console.
If you want information about the selected data to be reflected in the graph in some way, refer to the following document:
https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-selections
This method also requires knowledge of JavaScript.
If you want to complete with Python only, you must launch bokeh server.
For more information, please visit:
https://qiita.com/shotoyoo/items/7f342b2f4b8047a57e29
© 2023 OneMinuteCode. All rights reserved.