[英]Bokeh: how to dynamically display nominal values for X-axis with stream?
I'm trying to run a simple bokeh server, where I constantly receive a value and corresponding string, updating the data using stream()
.我正在尝试运行一个简单的散景服务器,在那里我不断收到一个值和相应的字符串,并使用
stream()
更新数据。 The y-axis is a real number, while the x-axis is a nominal value which I do not know in advance. y 轴是实数,而 x 轴是我事先不知道的标称值。 Currently, I have something similar to this example:
目前,我有类似这个例子的东西:
from bokeh.layouts import column, gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure
import random
import string
def update():
global val
val = int(val + 10 * random.randint(-100, 100)/50)
s = ''.join(random.choice(string.ascii_letters)) # A random string I have no control over
log.append(s)
source.stream(new_data=dict(value=[val], time=[len(log)], log=[s]))
val = 100
log = []
source = ColumnDataSource(dict(value=[], time=[], log=[]))
p = figure()
p.x_range.follow = "end"
p.x_range.follow_interval = 200
p_curr = p.line(x='time', y='value', line_width=3, source=source)
curdoc().add_root(column(gridplot([[p]])))
curdoc().add_periodic_callback(update, 50)
Run with: bokeh serve --show app.py
运行:
bokeh serve --show app.py
The only thing I want to change is that instead of displaying an integer (in my case, the value of the time
column) the figure should display for each data point the value of s
associated with it (ie, the value of the command
column).我唯一要更改的是,不是显示 integer (在我的情况下,
time
列的值),该图应该为每个数据点显示与其关联的s
值(即command
列的值)。 The position of the line itself should be identical, just each x-axis tick should be replaced with the corresponding string that was derived each update()
.行本身的 position 应该是相同的,只是每个 x 轴刻度应该替换为每个
update()
派生的相应字符串。
This can be done by using SingleIntervalTicker
and FuncTickFormatter
.这可以通过使用
SingleIntervalTicker
和FuncTickFormatter
来完成。 Add following code after p = figure()
:在
p = figure()
之后添加以下代码:
from bokeh.models import SingleIntervalTicker, FuncTickFormatter
p.xaxis.ticker = SingleIntervalTicker(interval=1, num_minor_ticks=0)
p.xaxis.formatter = FuncTickFormatter(args=dict(source=source), code='''
if(tick < 0){
return '';
}
if(tick < source.data.log.length){
return source.data.log[tick];
}
else{
return '';
}
''')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.