2017-12-29 19 views
1

Bokeh 플롯에서 선택한 데이터 포인트를 쓰려고합니다. Button을 클릭 할 때마다 ColumnDataSourceselected 속성에 액세스하여 선택한 데이터 요소를 가져 오는 것이 좋습니다.Bokeh 플롯 선택한 데이터가 작동하지 않습니다.

다음은 달성하려는 기능의 모델입니다.

기대하십시오 '선택한 점'버튼을 클릭 한 후, 파일 /tmp/datapoints.json 선택 점의 목록 (있는 경우)를 포함하는 생성 될 것이다.

실재 : 아니요 /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 도구

답변

0

이처럼 작업 할 수 있습니다

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)) 

내가 처음 '/'에서 '/tmp/datapoints.json'을 제거했다 json.dump 작동하려면.

+0

감사합니다. @ 요리스. 나는 내가 선택 도구 (들)을 놓치고있는 것을 보았다! – Brandt