Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 刷新后Bokeh实时更新x_轴_Python_Real Time_Visualization_Updates_Bokeh - Fatal编程技术网

Python 刷新后Bokeh实时更新x_轴

Python 刷新后Bokeh实时更新x_轴,python,real-time,visualization,updates,bokeh,Python,Real Time,Visualization,Updates,Bokeh,从最近几天开始,我一直在尝试使用Bokeh来绘制实时数据,并在.html上显示,以便在网页中使用。我成功地根据自己的需要改编了一个博克的例子。我在地块上使用了50个元素的缓冲区,我注意到以下行为: 1) 如果我运行脚本并进入浏览器,x_范围完全适应输入数据,一切正常 2) 如果单击浏览器上的“刷新”,x_范围将停止以适应传入数据,并冻结到最后一个值 我试图将x_轴强制为初始值和结束值,但可视化效果不佳 我认为我没有正确理解“刷新”命中对我的代码有什么影响,以及如何解决这个问题 """ To vi

从最近几天开始,我一直在尝试使用Bokeh来绘制实时数据,并在.html上显示,以便在网页中使用。我成功地根据自己的需要改编了一个博克的例子。我在地块上使用了50个元素的缓冲区,我注意到以下行为:

1) 如果我运行脚本并进入浏览器,x_范围完全适应输入数据,一切正常

2) 如果单击浏览器上的“刷新”,x_范围将停止以适应传入数据,并冻结到最后一个值

我试图将x_轴强制为初始值和结束值,但可视化效果不佳

我认为我没有正确理解“刷新”命中对我的代码有什么影响,以及如何解决这个问题

""" 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服务器: bokeh serve--允许websocket来源=本地主机:8000 然后通过以下方式将示例加载到Bokeh服务器 运行脚本: python.py 最后,启动一个简单的web服务器 通过运行: python-msimplehttpserver(python2) 或 python-mhttp.server(python3) 在此目录中。导航到 http://localhost:8000/animated.html """ 来自未来导入打印功能 输入io 从numpy导入pi、cos、sin、linspace、roll 从bokeh.client导入推送会话 从bokeh.embed导入服务器\u会话 从bokeh.plotting导入图,curdoc 从bokeh.models导入ColumnDataSource fa=打开('accelerator.txt','r') source=ColumnDataSource(data=dict(x=[],y=[])) fg=图形(宽度=250,绘图高度=250,标题=“RT测试”) fg.line(x='x',y='y',color=“olive”,source=source) fg.x_range.follow=“结束” #可视化尺度与美学 fg.xgrid.grid\u line\u color=无 fg.ygrid.grid\u line\u color=None fg.background\u fill\u color=“雪” #将绘图添加到curdoc curdoc().添加根目录(fg) #打开一个会话,使本地文档与服务器保持同步 会话=推送会话(curdoc()) html=”“” % “%server\u session(fg,session\u id=session.id,relative\u url=False) 使用io.open(“animated.html”,mode='w+',encoding='utf-8')作为f: f、 编写(html) 打印(文档) def update(): line=fa.readline().split(',') x=浮动(第[0]行) y=浮动(第[1]行) 打印(x,y) #为所有列构造新值,并传递到流 新数据=dict(x=[x],y=[y]) source.stream(新的_数据,滚动=50) curdoc().添加定期回调(更新,100) session.loop_直到_关闭()#永远运行
这种使用Bokeh服务器的方式,实际代码在一个单独的进程中运行,并调用
session.loop\u直到\u关闭
,在最强烈的意义上是不鼓励的。在下一个版本中,所有此类示例都将被删除,文档中不再提及这种方法。这种用法在很多方面都有其固有的缺陷,我要说,长期以来如此突出地展示它是我们的错误。它偶尔对测试有用,但除此之外没有其他用途

那么,使用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
命令运行此脚本,则任何刷新都将为您提供此应用程序的全新会话。同样值得一提的是,用这种方式编写的代码相当简单和简短


这些类型的应用程序可以嵌入Jupyter笔记本、Flask和其他web应用程序中,或者制作成使用
python
而不是
bokeh-serve
运行的“常规”python脚本。有关更多信息,请参阅《用户手册》中的

谢谢你的详细解释。我最初使用了您提出的方法,但最后得出的结论是不适合我的需要(或者更可能的是,我不完全了解如何集成)。问题是,任何刷新都将重新启动新会话,从而从一开始就启动实时流。我想要的是在网页中集成实时。。。如果用户在菜单上来回移动并返回实时可视化窗口,我不希望重新启动整个会话,而是希望继续使用最后的值(这就是我尝试使用此值的原因)。上面的代码总是从一开始就开始读取数据
'accelerator.txt'
,但它不必这样做。如果你不断地将数据倾倒到该文件中,你可以改变逻辑,从结尾开始阅读,或者在中间适合你的需要。嗨,杜达,谢谢你的精确回答。有没有办法让bokeh python文件中有可重新加载的HTML文件,而不是在浏览器中调用无限循环并按HTML文件中的刷新?