在Bokeh(0.11)服务器应用程序中刷新/更新图的快速正确方法是什么? [英] What is a fast and proper way to refresh/update plots in Bokeh (0.11) server app?

查看:137
本文介绍了在Bokeh(0.11)服务器应用程序中刷新/更新图的快速正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bokeh(v0.11)服务应用程序,该应用程序使用数据框中的(x,y)坐标生成散点图.我想添加交互,以便当用户在图上选择点或在文本框中输入逗号分隔点的名称(即"p55,p1234")时,这些点在散点图上将变为红色.

I have a bokeh (v0.11) serve app that produces a scatter plot using (x,y) coordinates from a data frame. I want to add interactions such that when a user either selects points on the plot or enters the name of comma-separated points in the text box (ie. "p55, p1234"), then those points will turn red on the scatter plot.

我找到了一种完成此操作的方法(下面的策略3),但是对于大型数据帧而言,它的运行速度非常慢.我认为有更好的方法.谁能帮我吗?我是否缺少一些明显的函数调用?

I have found one way to accomplish this (Strategy #3, below) but it is terribly slow for large dataframes. I would think there is a better method. Can anyone help me out? Am I missing some obvious function call?

  • 策略1 (对于1个点,<1ms)钻取到现有图的ColumnDataSource数据,并尝试更改所选点.
  • 策略2 (每100点〜70ms)用新创建的ColumnDataSource覆盖图的现有ColumnDataSource.
  • 策略3 (每100点约400毫秒)是策略2,然后重新创建 图.
  • Strategy 1 (<1ms for 100 points) drills into the ColumnDataSource data for the exist plot and attempts to change the selected points.
  • Strategy 2 (~70ms per 100 points) overwrites the plot's existing ColumnDataSource with a newly created ColumnDataSource.
  • Strategy 3 (~400ms per 100 points) is Strategy 2 and then it re-creates the figure.

代码存放在pastebin上: http://pastebin.com/JvQ1UpzY 最相关的部分复制在下面.

Code is deposited on pastebin: http://pastebin.com/JvQ1UpzY Most relevant portion is copied below.

def refresh_graph(self, selected_points=None, old_idxs=None, new_idxs=None):
    # Strategy 1: Cherry pick current plot's source.
    # Compute time for 100 points: < 1ms.
    if self.strategy == 1:
        t1 = datetime.now()
        for idx in old_idxs:
            self.graph_plot.data_source.data['color'][idx] = 'steelblue'
        for idx in new_idxs:
            self.graph_plot.data_source.data['color'][idx] = 'red'
        print('Strategy #1 completed in {}'.format(datetime.now() - t1))
    else:
        t3 = datetime.now()
        self.coords['color'] = 'steelblue'
        self.coords.loc[selected_points, 'color'] = 'red'
        new_source = bkmodels.ColumnDataSource(self.coords)
        self.graph_plot = self.graph_fig.scatter('x', 'y', source=new_source, color='color', alpha=0.6)
        print('Strategy #3 completed in {}'.format(datetime.now() - t3))
    return

理想情况下,我希望能够使用策略1 ,但是它似乎不允许这些点在客户端浏览器中刷新.

Ideally, I would like to be able to use Strategy #1, but it does not seem to allow the points to refresh within the client browser.

感谢您的帮助!

仅供参考:我正在使用RHEL 6.X

FYI: I am using RHEL 6.X

推荐答案

如果您正在数据,那么这里有一个相关的答案:

If you are streaming data, then there is a related answer here: Timeseries streaming in bokeh

如果您需要一次更新所有内容,则可以执行此操作,而我的建议是您的策略1 ,例如,在这里:

If you need update everything at once, then you can do that, and my suggestion is your Strategy 1, which is demonstrated, e.g. here:

https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py

要特别注意的一点是,您真的必须一次性更新所有source.data.假设之一是列数据源的所有列始终具有相同的长度.更新各个列可能会违反该假设,这可能会引起问题.因此,您想一次更新所有内容,例如:

The particular thing to note is that you really have to update all of source.data in one go. One of the assumptions is that all the columns of a column data source always have the same length. Updating individual columns runs the risk of breaking this assumption, which can cause problems. So you want to update all at once, with something like:

# Generate the new curve
x = np.linspace(0, 4*np.pi, N)
y = a*np.sin(k*x + w) + b

source.data = dict(x=x, y=y)

这篇关于在Bokeh(0.11)服务器应用程序中刷新/更新图的快速正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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