【发布时间】:2015-10-04 16:08:39
【问题描述】:
我在一个实验中使用 Bokeh 来实时绘制数据,该库提供了一种方便的方法来做到这一点。
这是我完成此任务的代码的 sn-p:
# do the imports
import pandas as pd
import numpy as np
import time
from bokeh.plotting import *
from bokeh.models import ColumnDataSource
# here is simulated fake time series data
ts = pd.date_range("8:00", "10:00", freq="5S")
ts.name = 'timestamp'
ms = pd.Series(np.arange(0, len(ts)), index=ts)
ms.name = 'measurement'
data = pd.DataFrame(ms)
data['state'] = np.random.choice(3, len(ts))
data['observation'] = np.random.choice(2, len(ts))
data.reset_index(inplace=True)
data.head()
接下来我已经使用下面的截图将数据实时推送到服务器了
output_server("observation")
p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
x = np.array(data.head(2).timestamp, dtype=np.datetime64)
y = np.array(data.head(2).observation)
p.diamond_cross(x,y, size=30, fill_color=None, line_width=2, name='observation')
show(p)
renderer = p.select(dict(name="observation"))[0]
ds = renderer.data_source
for mes in range(len(data)):
x = np.append(x, np.datetime64(data.loc[mes].timestamp))
y = np.append(y, np.int64(data.loc[mes].observation))
ds.data["x"] = x
ds.data["y"] = y
ds._dirty = True
cursession().store_objects(ds)
time.sleep(.1)
这会产生非常好的结果,但是我需要根据值更改每个数据点的颜色。
在这种情况下,条件是状态变量,它取三个值——0、1 和 2。所以我的数据应该能够反映这一点。 我花了几个小时试图弄清楚(诚然我对 Bokeh 很陌生),任何帮助都将不胜感激。
【问题讨论】:
标签: python plot time-series real-time bokeh