我想用堆叠条创建一个实时更新图。会议期间必须添加新栏。但更新功能仅在我刷新浏览器时才有效。这是我的代码:
import numpy as np
import datetime
from bokeh.plotting import figure, ColumnDataSource, curdoc
import random
x = 0
def get_data():
global x
x = x + random.random()
new = {
'x':[x],
'y1':[random.random()],
'y2':[random.random()]
}
return new
type = ["y1", "y2"]
colors = ["blue", "yellow"]
source = ColumnDataSource({'x':[], 'y1':[], 'y2':[]})
def update():
new=get_data()
source.stream(new)
p = figure(plot_width=800, plot_height=400)
p.vbar_stack(type, x='x', width=0.5, color=colors, source=source)
curdoc().add_periodic_callback(update, 60)
curdoc().add_root(p)