下面我有以下python代码,基于两个Bokeh示例:
import numpy as np
from numpy import pi
from bokeh.client import push_session
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import TableColumn, DataTable
from bokeh.io import show, vform
x = np.linspace(0, 4*pi, 80)
y = np.sin(x)
p = figure()
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)
source = ColumnDataSource(data=dict(x=x, y=y))
columns = [TableColumn(field="x", title="X"),TableColumn(field="y", title="Y")]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
session = push_session(curdoc())
@cosine(w=0.03)
def update(step):
r2.data_source.data["y"] = y * step
r2.glyph.line_alpha = 1 - 0.8 * abs(step)
layout = vform(data_table)
show(layout)
curdoc().add_periodic_callback(update, 50)
session.show() # open the document in a browser
session.loop_until_closed()
执行代码时,您将看到振荡的正弦曲线和静态数据表。如何修改代码,使用曲线的更改函数值动态更新表?提前谢谢!
答案 0 :(得分:1)
在更新代码中,您只更新了该行的数据源,您需要:
第二种可能更有效,但取决于您的需求。
它看起来像这样:
p = figure()
source = ColumnDataSource(data=dict(x=x, y=y))
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
# This is the major change - using the same source here as for data_table
r2 = p.line(x='x', y='y', color="navy", line_width=4, source=source)
columns = [TableColumn(field="x", title="X"),TableColumn(field="y", title="Y")]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
我已经确认这会导致两者都更新 - 虽然我不确定这是你想要的