2016-05-12 54 views
0

我有下面的例子來自reddit線程的實時流Bokeh 0.10.0。從Bokeh 0.10.0移植代碼到0.11.0

import time 
from random import shuffle 
from bokeh.plotting import figure, output_server, cursession, show 

# prepare output to server 
output_server("animated_line") 

p = figure(plot_width=400, plot_height=400) 
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], name='ex_line') 
show(p) 

# create some simple animation.. 
# first get our figure example data source 
renderer = p.select(dict(name="ex_line")) 
ds = renderer[0].data_source 

while True: 
    # Update y data of the source object 
    shuffle(ds.data["y"]) 

    # store the updated source on the server 
    cursession().store_objects(ds) 
    time.sleep(0.5) 

我知道0.11.0版本沒有cursession。代碼在Bokeh 0.11.0中會是什麼樣子?這是我的嘗試。我錯過了什麼嗎?基本上,我想要下面的代碼做的是作爲一個應用程序運行,以便當我提供實時流數據時,我可以更新源並實時繪製它。

from bokeh.models import ColumnDataSource, HoverTool, HBox, VBoxForm 
from bokeh.plotting import Figure, output_file, save 
from bokeh.embed import file_html 
from bokeh.models import DatetimeTickFormatter, HoverTool, PreText 
from bokeh.io import curdoc 
from bokeh.palettes import OrRd9, Greens9 

plot = Figure(logo=None, plot_height=400, plot_width=700, title="", 
     tools=["resize,crosshair"]) 

source = ColumnDataSource(data=dict(x=[], y=[])) 
plot.line([1,2,3], [10,20,30], source=source, legend='Price', line_width=1, line_color=OrRd9[0]) 

curdoc().add_root(HBox(plot, width=1100)) 

回答

1

你可能想添加一個週期性的回調,是這樣的:

def update(): 
    ds.data["y"] = shuffle(y) 

curdoc().add_periodic_callback(update, 500) 

而且你確實需要把數據轉換成列數據源,並告訴line你想要的列使用,而不是傳遞列表文字到figure

source = ColumnDataSource(data=dict(x=[1,2,3], y=[10,20,30])) 
plot.line('x', 'y', source=source, legend='Price', 
      line_width=1, line_color=OrRd9[0]) 
+0

我想了解參數'步驟'。它是否包含我們在'add_periodic_callback'中傳遞的值'500'?如果我在更新函數中的任何地方都沒有使用這個變量'step',它給了我下面的錯誤'錯誤:bokeh.util.tornado:週期性回調拋出的錯誤:TypeError('update()只需要1個參數)',)' – Veenit

+0

對不起,這是一個複製粘貼錯誤。有一些裝飾器可以在每次調用時自動爲回調提供「count」類型值,但對於基本用法,回調函數不帶參數。我已經相應地更新了答案。 – bigreddot