我正在使用 Bokeh 生成交互式时间序列图。可以同时显示 n 个系列。每个系列将显示从 t= 0 到 t = x,其中 x 是滑块创建的值。
我使用 ColumnDataSource 来包含所有内容,使用 MultiLine 字形显示系列,使用 Slider 用于滑块,使用 CustomJS 来控制更新交互。
from bokeh.models import CustomJS, ColumnDataSource, Slider, Plot
from bokeh.models.glyph import MultiLine
from bokeh.io import show
from bokeh.layouts import column
data_dict = {'lons':[[-1.0, -1.1, -1.2, -1.3, -1.4], [-1.0, -1.1, -1.25, -1.35, -1.45]], 'lats':[[53.0, 53.1, 53.2, 53.3, 53.4], [53.05, 53.15, 53.25, 53.35, 53.45]]}
source = ColumnDataSource(data_dict)
p = Plot(title = None, plot_width = 400, plot_height = 400)
glyph = MultiLine(xs = 'lons', ys = 'lats')
p.add_glyph(source, glyph)
callback = CustomJS(args = dict(source = source), code = """
var data = source.data;
var time = time.value;
var lons = data['lons']
var lats = data['lats']
var runners = lons.length()
var new_lons = []
var new_lats = []
for(i=0; i<runners; i++{
var runner_lons = lons[i].slice(0, time)
var runner_lats = lats[i].slice(0, time)
new_lons.push(runner_lons)
new_lats.push(runner_lats)
}
lons = new_lons
lats = new_lats
source.change.emit();
""")
slider = Slider(start = 0, , end = 5, value = 0, step = 1, callback = callback)
layout = column(p, slider)
callback.args["time"] = slider
show(layout)
此代码呈现图形,绘制的两条线覆盖source.data.
移动滑块将按预期更新lons&中的数据lats,但图形显示不会更新。
非常感谢您的指点,建议,建议,解释!