gpt4 book ai didi

python - 刷新后Bokeh实时更新x_axis

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:35 27 4
gpt4 key购买 nike

从最近几天开始,我一直在尝试使用 Bokeh 绘制实时数据并显示在 .html 上,以便嵌入网页中。我已成功地根据我的需要调整了 Bokeh 示例之一。我在绘图上使用了 50 个元素的缓冲区,并且注意到以下行为:

1)如果我运行脚本并转到浏览器,x_range 会完全适应传入数据并且一切正常

2)如果我点击浏览器上的“刷新”,x_range 就会停止适应传入数据并卡住到最后一个值。

我尝试强制 x_axis 为初始值和最终值,但可视化效果很差。

我认为我没有正确理解“刷新”点击对我的代码有何影响以及如何解决此问题。

""" To view this example, first start a Bokeh server:

bokeh serve --allow-websocket-origin=localhost:8000

And then load the example into the Bokeh server by
running the script:

python animated.py

in this directory. Finally, start a simple web server
by running:

python -m SimpleHTTPServer (python 2)

or

python -m http.server (python 3)

in this directory. Navigate to

http://localhost:8000/animated.html

"""
from __future__ import print_function

import io

from numpy import pi, cos, sin, linspace, roll

from bokeh.client import push_session
from bokeh.embed import server_session
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource

fa = open('Accelerometer.txt', 'r')

source = ColumnDataSource(data=dict(x=[], y=[]))
fg = figure(width=250, plot_height=250, title="RT-Test")
fg.line(x='x', y='y', color="olive", source=source)
fg.x_range.follow = "end"

# Visualization scale and aesthetics
fg.xgrid.grid_line_color = None
fg.ygrid.grid_line_color = None
fg.background_fill_color = "snow"

# add the plot to curdoc
curdoc().add_root(fg)

# open a session which will keep our local doc in sync with server
session = push_session(curdoc())

html = """
<html>
<head></head>
<body>
%s
</body>
</html>
""" % server_session(fg, session_id=session.id, relative_urls=False)

with io.open("animated.html", mode='w+', encoding='utf-8') as f:
f.write(html)

print(__doc__)

def update():
line = fa.readline().split(',')
x = float(line[0])
y = float(line[1])
print(x, y)

# construct the new values for all columns, and pass to stream
new_data = dict(x=[x], y=[y])
source.stream(new_data, rollover=50)

curdoc().add_periodic_callback(update, 100)

session.loop_until_closed() # run forever

最佳答案

这种 Bokeh 服务器的使用方式,即实际代码在单独的进程中运行并调用 session.loop_until_close最强烈地不鼓励 。在下一个版本中,所有此类示例都将被删除,并且从文档中删除对此方法的提及。这种用法在很多方面本质上都是较差的,as outlined here ,我想说,长期如此突出地展示它是我们的一个错误。它偶尔对测试有用,但除此之外没有什么用处。

那么使用 Bokeh 服务器的好/预期方法是什么?答案是让 Bokeh 应用在 Bokeh 服务器本身中运行,与上面的代码不同。这可以通过多种方式完成,但一种常见的方式是编写一个简单的脚本,然后使用

执行该脚本
bokeh serve -show myapp.py

我无权访问您的“Accelerate.py”数据集,但更新代码的粗略过程如下:

# myapp.py     
from numpy import pi, cos, sin, linspace, roll

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource

fa = open('Accelerometer.txt', 'r')

source = ColumnDataSource(data=dict(x=[], y=[]))
fg = figure(width=250, plot_height=250, title="RT-Test")
fg.line(x='x', y='y', color="olive", source=source)
fg.x_range.follow = "end"

fg.xgrid.grid_line_color = None
fg.ygrid.grid_line_color = None
fg.background_fill_color = "snow"

curdoc().add_root(fg)

def update():
line = fa.readline().split(',')
x = float(line[0])
y = float(line[1])

# construct the new values for all columns, and pass to stream
new_data = dict(x=[x], y=[y])
source.stream(new_data, rollover=50)

curdoc().add_periodic_callback(update, 100)

现在,如果您使用 bokehserve 命令运行此脚本,则任何刷新都将为您提供此应用程序的全新 session 。以这种方式编写的代码相当简单和简短,这一点也是毫无值(value)的。

这些类型的应用程序可以嵌入 Jupyter 笔记本、Flask 和其他 Web 应用程序中,或者制作成使用 python 而不是 bokehserve 运行的“常规”Python 脚本。欲了解更多信息,请参阅Running a Bokeh Server在用户指南中。

关于python - 刷新后Bokeh实时更新x_axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48338106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com