I want to get information about the selected data using the box select function of the bokeh.

Asked 1 years ago, Updated 1 years ago, 50 views

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)

python bokeh

2022-09-30 11:30

1 Answers

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.

Enter a description of the image here

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

Run Environment

  • Python 3.9.7
    • bokeh2.4.7
    • jupyter 1.0.0
  • Firefox 94.0 (64bit)
  • XUbuntu 18.04
  • bokeh2.4.7
  • jupyter 1.0.0


2022-09-30 11:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.