记下散景图所选数据不起作用 [英] Write down Bokeh plot selected data not working
问题描述
我正在尝试从散景图中写入选定的数据点.
这个想法是,只要单击Button
,就可以访问ColumnDataSource
selected
属性以获取选定的数据点.
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 clicked.
下面是我要实现的功能的模型.
Below goes a model of the functionality I'm trying to achieve.
期望:单击"选定点"按钮后,将创建一个文件/tmp/datapoints.json
,其中包含选定点的列表(如果有).
Expectation: after clicking the 'Selected points' button, a file /tmp/datapoints.json
would be created containing the list of points selected (if any).
现实:否/tmp/datapoints.json
.
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.models import ColumnDataSource, Button
from bokeh.layouts import column
# setup plot
fig = figure(title='Select points',
plot_width=300, plot_height=200)
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 interaction
def print_datapoints(attr, old, new):
with open('/tmp/datapoints.json', 'w') as f:
import json
json.dump(src.selected, f)
btn = Button(label='Selected points', button_type='success')
btn.on_click(print_datapoints)
curdoc().add_root(column(btn,fig))
我想念什么?
谢谢.
推荐答案
使用lasso_select工具,您可以像这样工作:
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 interaction
def print_datapoints():
indices=src.selected['1d']['indices']
results=data.iloc[indices]
resultsDict=results.to_dict()['x']
resultString=str(resultsDict)
with open('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))
要使json.dump正常工作,我必须从'/tmp/datapoints.json'中删除第一个'/'.
To make the json.dump work i had to remove the first '/' from '/tmp/datapoints.json' .
这篇关于记下散景图所选数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!