Python调用更新函数时散景图显示两条线

Python Bokeh graph displays two lines when update function called

我一直在使用 python 散景功能,我希望在将代码输入到 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)

GitHub 上的示例目录中有几个示例笔记本展示了如何更新现有字形的数据源:

https://github.com/bokeh/bokeh/tree/master/examples/howto/notebook_comms

简而言之,您想更新数据源:

source.data = new_data_dict
push_notebook()