Write Down Bokeh Plot Selected Data Not Working
I am trying to write the selected datapoints from a Bokeh plot. The idea is to access ColumnDataSource selected property to get the selected data points whenever the Button is clic
Solution 1:
with the lasso_select tool you can work it like this:
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Button
from bokeh.layouts import column
# setup plot
tools = "pan,wheel_zoom,lasso_select,reset"
fig = figure(title='Select points',
plot_width=300, plot_height=200,tools=tools)
import numpy as np
x = np.linspace(0,10,100)
y = np.random.random(100) + x
import pandas as pd
data = pd.DataFrame(dict(x=x, y=y))
# define data source
src = ColumnDataSource(data)
# define plot
fig.circle(x='x', y='y', source=src)
# define interactiondefprint_datapoints():
indices=src.selected['1d']['indices']
results=data.iloc[indices]
resultsDict=results.to_dict()['x']
resultString=str(resultsDict)
withopen('tmp/datapoints.json', 'w') as f:
import json
json.dump(resultString, f)
btn = Button(label='Selected points', button_type='success')
btn.on_click(print_datapoints)
curdoc().add_root(column(btn,fig))
To make the json.dump work i had to remove the first '/' from '/tmp/datapoints.json' .
Post a Comment for "Write Down Bokeh Plot Selected Data Not Working"