选择Datatable(Bokeh)以更新绘图

时间:2018-02-08 20:05:59

标签: plot datatable bokeh

当选择不同的Datatable行时,由以下代码生成的绘图不会改变。想知道我哪里出错了。我想根据选定的数据行显示绘图。感谢

from datetime import date
from random import randint

from bokeh.io import output_file, show, curdoc
from bokeh.plotting import figure
from bokeh.layouts import widgetbox, row
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn,Button

output_file("data_table.html")

data = dict(
    dates=[date(2014, 3, i+1) for i in range(10)],
    downloads=[randint(0, 100) for i in range(10)],
)

def update(attr,old,new):
    #set inds to be selected
inds = [1]
source.selected = {'0d': {'glyph': None, 'indices': []},
                            '1d': {'indices': inds}, '2d': {}}
# set plot data
plot_dates = [data['dates'][i] for i in inds]
plot_downloads = [data['downloads'][i] for i in inds]
plot_source.data['dates'] = plot_dates
plot_source.data['downloads'] = plot_downloads


source = ColumnDataSource(data)
plot_source = ColumnDataSource({'dates':[],'downloads':[]})


#table_button = Button(label="Press to set", button_type="success")
columns = [
    TableColumn(field="dates", title="Date", formatter=DateFormatter()),
    TableColumn(field="downloads", title="Downloads"),
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

p = figure(plot_width=400, plot_height=400)

# add a circle renderer with a size, color, and alpha
p.circle('dates','downloads',source=plot_source, size=20, color="navy", alpha=0.5)

source.on_change('selected',update)
curdoc().add_root(row([data_table,p]))

1 个答案:

答案 0 :(得分:0)

只需更改上述代码 -

def update(attr,old,new):
data =  Hits_File
selected = source.selected['1d']['indices']
if selected:
   data  = data.iloc[selected, :]
   data.columns = data.columns.str.strip()
   print("<{}>".format(data.columns[5]))
   testvalue = data.iloc[0,5]
   BarrierLine.location = data.iloc[0,7] 
   update_plot(data,testvalue)

def update_plot(data,testvalue):
src_data_table = ColumnDataSource(data)
plot_source.data.update(src_data_table.data)
data1 =  get_all_price_dataset(Combined_AllDates,testvalue)
print(testvalue)
price_src_data_table = ColumnDataSource(data1)
price_plot_source.data.update(price_src_data_table.data)