我有以下代码:
import random
import datetime as dt
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
source = ColumnDataSource(dict(
x = [], y = []
))
p = figure(plot_height=250, plot_width=400, tools="save",
x_axis_type="datetime", y_axis_location="right")
p.x_range.follow = "end"
p.x_range.follow_interval = 5000 # ms
p.line(x='x', y='y', color='red', source=source)
cur_time = dt.datetime.now()
step = dt.timedelta(seconds=1)
def update():
global cur_time, step
new_data = dict(x=[], y=[])
new_data['x'].append(cur_time)
new_data['y'].append(random.randint(1, 100))
cur_time += step
source.stream(new_data, 60) # how many data are saved
curdoc().add_root(p)
curdoc().add_periodic_callback(update, 500)
我想剪切数据,因此我将翻转参数添加到stream method。起初可以:
但过了一会儿,这条线开始转移:
为什么会这样?以及如何解决这个问题?
感谢您的帮助!
答案 0 :(得分:0)
import random
import datetime as dt
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
source = ColumnDataSource(dict(
x = [], y = []
))
p = figure(plot_height=250, plot_width=400, tools="save",
x_axis_type="datetime", y_axis_location="right")
# p.x_range.follow = "end" #***** THIS IS YOUR ISSUE *****
p.x_range.follow_interval = 500 # ms
p.line(x='x', y='y', color='red', source=source)
cur_time = dt.datetime.now()
step = dt.timedelta(seconds=1)
def update():
global cur_time, step
new_data = dict(x=[], y=[])
new_data['x'].append(cur_time)
new_data['y'].append(random.randint(1, 100))
cur_time += step
source.stream(new_data, 60) # how many data are saved
curdoc().add_root(p)
curdoc().add_periodic_callback(update, 50)
查看我的编辑。不需要x_range.follow,请参阅http://bokeh.pydata.org/en/0.11.1/docs/reference/models/ranges.html