使用散景流两个线图

时间:2022-01-07 00:36:07

I would like to create a visualization where there are two line graphs which are updated with one new point per line graph per second. The result will be something like this.

我想创建一个可视化,其中有两个线图,每秒更新每个线图一个新点。结果将是这样的。

I have recently read about bokeh and found out that it can be used in visualizing streams of data in real time. However don't know how to code in it yet.

我最近阅读了关于散景的内容,发现它可以用于实时显示数据流。但是不知道如何在其中编码。

I would appreciate it if someone can show me how this task can be done using bokeh. thanks!

如果有人能告诉我如何使用散景完成这项任务,我将不胜感激。谢谢!

2 个解决方案

#1


11  

For bokeh-0.11.1:

Basically, you need to run you python app in the bokeh server. Then anyone can connect to the server and view the graph in realtime.

基本上,您需要在散景服务器中运行python应用程序。然后任何人都可以连接到服务器并实时查看图表。

First, write your program. Use this code for example:

首先,编写你的程序。使用此代码示例:

# myplot.py
from bokeh.plotting import figure, curdoc
from bokeh.driving import linear
import random

p = figure(plot_width=400, plot_height=400)
r1 = p.line([], [], color="firebrick", line_width=2)
r2 = p.line([], [], color="navy", line_width=2)

ds1 = r1.data_source
ds2 = r2.data_source

@linear()
def update(step):
    ds1.data['x'].append(step)
    ds1.data['y'].append(random.randint(0,100))
    ds2.data['x'].append(step)
    ds2.data['y'].append(random.randint(0,100))  
    ds1.trigger('data', ds1.data, ds1.data)
    ds2.trigger('data', ds2.data, ds2.data)

curdoc().add_root(p)

# Add a periodic callback to be run every 500 milliseconds
curdoc().add_periodic_callback(update, 500)

Then run the server from the command line, with your program:

然后使用您的程序从命令行运行服务器:

C:\>bokeh serve --show myplot.py

This will open the browser with your realtime graph.

这将使用您的实时图表打开浏览器。

For all the details see the bokeh server documentation.

有关所有详细信息,请参阅散景服务器文档。

#2


4  

You can add scrolling to your graph by adding the following to the plot figure declaration:

您可以通过在绘图图形声明中添加以下内容来向图表添加滚动:

p = figure(plot_width=400, plot_height=400)
p.x_range.follow="end"
p.x_range.follow_interval = 20
p.x_range.range_padding=0

where the follow_interval = the number of points that accumulate on the on the graph before it starts scrolling. I believe you can set the visible range on the chart, as well. FYI I got the scrolling code from the OHLC example on the bokeh GitHub page found here: https://github.com/bokeh/bokeh/tree/master/examples/app The OHLC is an example of streaming data using the "...= new_data" technique that bigreddot mentioned.

其中,follow_interval =在开始滚动之前在图表上累积的点数。我相信你也可以在图表上设置可见范围。仅供参考我在散景GitHub页面上的OHLC示例中获得了滚动代码:https://github.com/bokeh/bokeh/tree/master/examples/app OHLC是使用“...”的流数据示例。 。= new_data“bigreddot提到的技术。

#1


11  

For bokeh-0.11.1:

Basically, you need to run you python app in the bokeh server. Then anyone can connect to the server and view the graph in realtime.

基本上,您需要在散景服务器中运行python应用程序。然后任何人都可以连接到服务器并实时查看图表。

First, write your program. Use this code for example:

首先,编写你的程序。使用此代码示例:

# myplot.py
from bokeh.plotting import figure, curdoc
from bokeh.driving import linear
import random

p = figure(plot_width=400, plot_height=400)
r1 = p.line([], [], color="firebrick", line_width=2)
r2 = p.line([], [], color="navy", line_width=2)

ds1 = r1.data_source
ds2 = r2.data_source

@linear()
def update(step):
    ds1.data['x'].append(step)
    ds1.data['y'].append(random.randint(0,100))
    ds2.data['x'].append(step)
    ds2.data['y'].append(random.randint(0,100))  
    ds1.trigger('data', ds1.data, ds1.data)
    ds2.trigger('data', ds2.data, ds2.data)

curdoc().add_root(p)

# Add a periodic callback to be run every 500 milliseconds
curdoc().add_periodic_callback(update, 500)

Then run the server from the command line, with your program:

然后使用您的程序从命令行运行服务器:

C:\>bokeh serve --show myplot.py

This will open the browser with your realtime graph.

这将使用您的实时图表打开浏览器。

For all the details see the bokeh server documentation.

有关所有详细信息,请参阅散景服务器文档。

#2


4  

You can add scrolling to your graph by adding the following to the plot figure declaration:

您可以通过在绘图图形声明中添加以下内容来向图表添加滚动:

p = figure(plot_width=400, plot_height=400)
p.x_range.follow="end"
p.x_range.follow_interval = 20
p.x_range.range_padding=0

where the follow_interval = the number of points that accumulate on the on the graph before it starts scrolling. I believe you can set the visible range on the chart, as well. FYI I got the scrolling code from the OHLC example on the bokeh GitHub page found here: https://github.com/bokeh/bokeh/tree/master/examples/app The OHLC is an example of streaming data using the "...= new_data" technique that bigreddot mentioned.

其中,follow_interval =在开始滚动之前在图表上累积的点数。我相信你也可以在图表上设置可见范围。仅供参考我在散景GitHub页面上的OHLC示例中获得了滚动代码:https://github.com/bokeh/bokeh/tree/master/examples/app OHLC是使用“...”的流数据示例。 。= new_data“bigreddot提到的技术。