我一直在使用python bokeh功能,当股票行情输入到TextInput部分时,我希望显示一张股票图。但是,就我而言,我完成这项工作的唯一方法是在更新函数中创建一个新的p.line,它将一个股票图覆盖在另一个股票图上。有没有一种方法可以更新我的源数据或更新功能,以便只显示输入库存的图形?
p=figure(
height=400,
x_axis_type='datetime',
title=(company+' ('+tickerstring+') '),
tools='pan, box_zoom, wheel_zoom, reset',
)
p.line('x', 'y', source=source)
line1=p.line(thedates, stockcloseprices)
p.grid.grid_line_color="white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.add_tools(HoverTool(
tooltips=[
("Date", "@x{%F}"),
('Close',"@y")
],
formatters={
'x':'datetime', # use 'datetime' formatter for 'date' field
},
mode='vline'
))
source = ColumnDataSource(data=dict(
x=thedates,
y=stockcloseprices
))
div = Div(text='<br><b> Key Points </b><br><br>'+percentagechange+'<br><br>'+performance,
width=200, height=100)
def update(f):
fstocksymbol=str(f.upper())
if fstocksymbol in stocksymbols:
p.title.text = (symbolsdictionary[fstocksymbol]).upper()+' ('+fstocksymbol+')'
tickerstring=fstocksymbol
firstfunction=stockname(tickerstring)
secondfunction=stockdata(firstfunction)
stockdates=[]
stockcloseprices=[]
for value in secondfunction:
stockdates.append(value[0])
stockcloseprices.append(value[4])
thedates = np.array(stockdates, dtype=np.datetime64)
p.line(thedates, stockcloseprices)
push_notebook()
elif fstocksymbol=='':
print('')
else:
print("")
interact(update, f='')
grid = gridplot([p, div, button], ncols=2, plot_width=570, plot_height=400)
show(grid, notebook_handle=True)
答案 0 :(得分:0)
有几个示例笔记本,它们显示了如何在GitHub上的examples目录中为现有字形更新数据源:
https://github.com/bokeh/bokeh/tree/master/examples/howto/notebook_comms
简而言之,您要更新数据源:
source.data = new_data_dict
push_notebook()