简体   繁体   中英

How can I get the coordinates of a click in a plot on python side?

I have a plot with circles on it and I want users to be able to select circles and then I want to do some stuff on python side depending on the circle.

I have tried JS callbacks but I get the error:

WARNING:bokeh.embed.util: You are generating standalone HTML/JS output, but trying to use real Python callbacks (ie with on_change or on_event). This combination cannot work. Only JavaScript callbacks may be used with standalone output. For more information on JavaScript callbacks with Bokeh, see:

http://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html Alternatively, to use real Python callbacks, a Bokeh server application may be used. For more information on building and running Bokeh applications, see: http://docs.bokeh.org/en/latest/docs/user_guide/server.html

Here's some code if you want to try to do it.

from bokeh.plotting import figure, output_file, show

output_file("openurl.html")

p = figure(plot_width=400, plot_height=400, tools="tap", title="Click the Dots")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    color=["navy", "orange", "olive", "firebrick", "gold"]
    ))

p.circle('x', 'y', color='color', size=20, source=source)```


I didnt quite understand your question, but you can use hover tool to add interaction, or LabelSet to display your x/y along with the dots.

from bokeh.plotting import figure
from bokeh.io import show, output_notebook
output_notebook()

p = figure(plot_width=400, plot_height=400, tools="tap, reset, hover", title="Click 
the Dots", tooltips = [('x', '@x'),('y','@y')])

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    color=["navy", "orange", "olive", "firebrick", "gold"]
    ))

p.circle('x', 'y', color='color', size=20, source=source)
show(p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM