【问题标题】:Bokeh line plot connects every points to the first point, what is wrong?散景线图将每个点连接到第一个点,有什么问题?
【发布时间】:2020-08-05 17:22:34
【问题描述】:

我正在尝试提供实时数据并根据日期时间绘制它。然而,每个点都通过一条线连接到第一个点。这里有什么问题?感谢您的任何帮助。 (我将其保存为 test.py 并在命令提示符下运行 bokeh serve --show test.py。)

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure
import numpy as np

x, y = [], []
source = ColumnDataSource(dict(time=x, data=y))

p = figure(x_axis_type='datetime')
p.line(x='time', y='data', source=source)
p.xaxis.formatter = DatetimeTickFormatter(days="%m/%d %H:%M", months="%m/%d %H:%M",
                                          hours="%m/%d %H:%M", minutes="%m/%d %H:%M")


def update():
    x.append(np.datetime64('now'))
    y.append(np.random.rand())
    source.stream(dict(time=x, data=y), 100)


curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)
curdoc().title = "random"

如下所示:

【问题讨论】:

  • @bigreddot,感谢您的回复。如您所见,它是从时间 0 开始在代码中生成的实时数据。没有其他数据。

标签: python-3.x plot windows-10 bokeh


【解决方案1】:

stream 用于将 new 点添加到数据源。您在每次更新中不断累积所有点,从而重新传输您已经发送的旧点。不需要附加任何东西,只发送实际的新点,没有别的:

def update():
    x = [np.datetime64('now')]
    y = [np.random.rand()]
    source.stream(dict(time=x, data=y), 100)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2021-07-02
    • 2021-12-14
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多