无法使用回调清除多线图形

Unable to clear multi-line figure with callback

我有一个带有线图的图形和另一个带有多线图的图形。当用户 select 从 Select 对象中选择新选项时,绘图会更新。 线图更新正确,与 ColumnDataSource 同步。但是,多线图从 pandas 数据框中提取信息。 问题是每次我 select 一个新选项时,线条都会在多线图上累积。

我试图在 on_change 回调函数中使用它,但不起作用: select.js_on_change('value',CustomJS(args=dict(plot=plot), code="""plot.reset.emit()"""))

我实际上应该在我的 onchange 回调中包含 CustomJS,但是我收到了一个错误。不知道怎么用。

###############
# callback function
###############
def callback(attr,old,new):
    selected = function_returning_DF_with_data_from_selected_users(select.value,times)
    source.data={'index': selected.index, 'count': selected.count}
    similar_time_users = get_top_5_neighbors_by_time(times,select.value)
    neighbors = function_that_returns_DF_with_selected_user_neighbors()

    numlines=len(neighbors.columns)
    mypalette=Spectral11[0:numlines]
    plot.multi_line(xs=[neighbors.index.values]*numlines,
                ys=[neighbors[name].values for name in neighbors, axis=1)],
                line_color=mypalette,
                line_width=1)


###############
# plotting
###############
select = Select(title="Select user: ", value='', options=user_list)

plot = figure(x_axis_label='Time of the day',y_axis_label='count')
plot.line(x= 'index', y='count', source=source, line_width=5) 

plot.multi_line(xs=[neighbors.index.values]*numlines,
            ys=[neighbors[name].values for name in neighbors, axis=1)],
            line_color=mypalette,
            line_width=1)

select.on_change('value',callback)
#select.js_on_change('value',CustomJS(args=dict(plot=plot), code="""plot.reset.emit()"""))

layout = row(widgetbox(select), plot)
curdoc().add_root(layout)

我希望有一个像第一个那样的情节: 然而,这是我多次 selecting 后得到的:

有什么建议吗? 非常感谢! 劳尔.

调用字形方法是加法。一遍又一遍地调用 multi_line 每次都会添加新的 multi-lines,而不删除之前添加的任何内容。对于这种用例,您应该做的是仅调用 multi_line(或您可能使用的任何字形)一次,然后仅更新数据源.例如:

source = ColumnDataSource(data=dict(xs=..., ys==...)
plot.multi_line(xs='xs', ys='ys', ..., source=source)

def callback(attr,old,new):
    source.data = new_data_dict