Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a point to a bokeh plot on click

I'm trying to set up a bokeh plot where the user can click on the plot to add a point. I've seen this example which uses the BoxSelectTool to add Rect glyphs to the plot, however I'm looking for a way to add circle glyphs centered at the click location. I would also like to then send these points back to the server side of things. Anyone have any experience doing something similar?

like image 448
jonchar Avatar asked Mar 13 '16 18:03

jonchar


People also ask

How do you plot multiple lines on bokeh?

Bokeh can be used to plot multiple lines on a graph. Plotting multiple lines on a graph can be done using the multi_line() method of the plotting module.

Is Bokeh a data visualization library?

Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets.


1 Answers

This works for me in Bokeh version 0.12.13:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Column
from bokeh.io import curdoc
from bokeh.events import DoubleTap

coordList=[]

TOOLS = "tap"
bound = 10
p = figure(title='Double click to leave a dot.',
           tools=TOOLS,width=700,height=700,
           x_range=(-bound, bound), y_range=(-bound, bound))

source = ColumnDataSource(data=dict(x=[], y=[]))   
p.circle(source=source,x='x',y='y') 

#add a dot where the click happened
def callback(event):
    Coords=(event.x,event.y)
    coordList.append(Coords) 
    source.data = dict(x=[i[0] for i in coordList], y=[i[1] for i in coordList])        
p.on_event(DoubleTap, callback)

layout=Column(p)

curdoc().add_root(layout)

(to run it save this script as something.py and run in a cmd: bokeh serve something.py --show )

enter image description here

like image 78
Joris Avatar answered Oct 02 '22 15:10

Joris