无法清除带有回调的多行图形 [英] Unable to clear multi-line figure with callback

查看:99
本文介绍了无法清除带有回调的多行图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带线图的图形,另一个带多线图的图形.当用户从选择"对象中选择新选项时,图将更新. 折线图正确更新,与ColumnDataSource同步.但是,多线图从熊猫数据框中提取信息. 问题在于,每当我选择一个新选项时,这些线就会累积在多线图中.

I have a figure with a line plot and another one with a multi-line plot. The plot updates when a user selects a new option from a Select object. The line plot updates correctly as is syncd with a ColumnDataSource. However, the multi-line plot pulls the info from a pandas dataframe. The problem is that the lines accumulate on the multi-line plot every time I select a new option.

我尝试在on_change回调函数中使用此方法,但无法正常工作: select.js_on_change('value',CustomJS(args = dict(plot = plot),code =""plot.reset.emit()""))

I tried to use this within the on_change callback function but won't work: select.js_on_change('value',CustomJS(args=dict(plot=plot), code="""plot.reset.emit()"""))

我实际上应该在我的onchange回调中包含CustomJS,但随后出现错误.不确定如何使用它.

I actually should include the CustomJS within my onchange callback but then I get an error. Not sure how to use it.

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

我希望有一个像第一个一样的图: 但是,这是我选择多次后得到的结果:

I expect to have a plot like the first plotted: However, this is what I'm getting after selecting multiple times:

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

Any suggestions? Many Thanks! Raul.

推荐答案

调用字形方法是 additive .反复调用multi_line每次都会添加新的多行,而不会删除以前添加的任何内容.对于这种用例,您应该做的只是一次 调用multi_line(或您可能正在使用的任何字形),然后再仅更新数据源.例如:

Calling glyph methods is additive. Calling multi_line over and over adds new multi-lines every time, without removing anything previously added. For this kind of use case, what you should do instead is call multi_line (or whatever glyph you might be using) only once, and then later, only update the data source. For example:

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

这篇关于无法清除带有回调的多行图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆