gpt4 book ai didi

python - [Python+ Bokeh ] : how to make a needle dial?

转载 作者:行者123 更新时间:2023-12-01 03:08:11 24 4
gpt4 key购买 nike

我需要一个针盘,显示实时值。该值改变针的角度,而不是位置。

到目前为止,我所能做的就是使用 ray()annular_wedge() 字形,但是这个解决方案有很多问题:前面的行不是删除,几次更新后它卡住,工件。完全无法使用。

这是我目前正在处理的精简代码。

from bokeh.driving import linear
from bokeh.plotting import figure, curdoc
import random

p = figure(plot_width=400, plot_height=400,
x_range=[0, 2], y_range=[0, 2],
x_axis_type=None, y_axis_type=None)

@linear()
def update(step):
ref_angle = random.random() * 2 * 3.14
p.ray(x=[1], y=[1], length=0.5, angle=ref_angle, color="red", line_width=2)

curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)

如您所见,我需要旋转光线更新ref_angle

有什么想法吗?

最佳答案

您可以创建一个包含 ref_angle 数据的 ColumnDataSource 对象,然后更新该数据,而不是在每次更新时绘制新射线。

如果您已经运行了 Bokeh 服务器(在不同的终端中运行 bokehserve),则以下代码应该可以正常工作。

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

p = figure(plot_width=400, plot_height=400,
x_range=[0, 2], y_range=[0, 2],
x_axis_type=None, y_axis_type=None)

source = ColumnDataSource(dict(ref_angle=[0]))
p.ray(x=1, y=1, length=0.5, angle='ref_angle', color="red",
line_width=2, source=source)

# open a session to keep our local document in sync with server
session = push_session(curdoc())

def update():
source.data.update(ref_angle=[random.random() * 2 * 3.14])

curdoc().add_periodic_callback(update, 1000)

session.show(p) # open the document in a browser
session.loop_until_closed() # run forever

关于python - [Python+ Bokeh ] : how to make a needle dial?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139543/

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