Python Bokeh使用滑块更新列中的特定列表
我必须展示一个梁的内力,它有不同的方位和不同的力的位置。为了将力移动到不同的位置,我想使用带滑块的bokeh的Python Bokeh使用滑块更新列中的特定列表,python,bokeh,Python,Bokeh,我必须展示一个梁的内力,它有不同的方位和不同的力的位置。为了将力移动到不同的位置,我想使用带滑块的bokeh的on_change功能。为此,我需要更改类Froce中我的ColumnDataSource中一个列表的值。到目前为止,我可以创建和绘制任何东西,但当我更改滑块的值时,它不会更改列表的值,也不会更改力在图形中的位置 这是calss力的构造器 from bokeh.plotting import figure from bokeh.models import ColumnDataSource
on_change
功能。为此,我需要更改类Froce
中我的ColumnDataSource
中一个列表的值。到目前为止,我可以创建和绘制任何东西,但当我更改滑块的值时,它不会更改列表的值,也不会更改力在图形中的位置
这是calss力的构造器
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
class Force:
list_force =[]
def __init__(self):
b = Beam.beam_list[0]
d = 0.2
self.source_force = dict(x=2*[b.get_x_left()], x_max=2*[(b.get_x_left() +
Beam.delta_x)], y=[b.get_y_top(),(b.get_y_top() + 1)], y_end_p=
[b.get_y_top(), (b.get_y_top() + d)],
x_p1=[b.get_x_left(), (b.get_x_left() + d)],
x_p2=[b.get_x_left(), (b.get_x_left() - d)] , d=[d,d], force=[400, 0])
self.CD_force = ColumnDataSource(data=self.source_force)
plot.line('x', 'y', source=self.CD_force, color="Blue")
plot.line('x_p1','y_end_p', source=self.CD_force, color="red")
plot.line('x_p2', 'y_end_p', source=self.CD_force, color="green")
Force.list_force.append(self)
print("Kraft wurde erstellt")
这是我想在回调事件中使用的更新函数
def update_position(sefl, attr, old, new):
N = new
force = Force.list_force[0]
s =slice(2)
force.CD_force.data['x'] = 2 * [N]
force.CD_force.patch({'x': [(s, [N, N])]})
我曾尝试更新列数据
以及目录
,但都没有成功
这里是绘图部分,我在这里创建了所有figuer、滑块、布局和回调函数
from Klassen import Force, Beam, Get_plot, Bearing
from bokeh.plotting import show
from bokeh.models import Slider
from bokeh.layouts import column
slider_pos = Slider(title="Positoin", start=0, end=8, value=0, step =0.1)
b = Beam(4,8)
Kraft = Force()
f = Bearing.Loslager(b)
slider_pos.on_change('value', Kraft.update_position)
layout = (column(slider_pos, Get_plot()))
show(layout)
我试着把它分解成这个基本代码,我想改变我创建的箭头的位置。我不确定更新功能
是否会因为某个时刻不同工作表中的不同类而受到影响。如果不是这个总结的话,那就是我在更新中遇到的问题
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Slider
from bokeh.layouts import column
plot = figure(plot_height=600, plot_width=400, x_range=(0, 10), y_range=
(0,15))
class Force:
list_force =[]
def __init__(self):
d = 0.2
self.source_force = dict(x=2*[2], x_max=2*[8], y=[3,4], y_end_p=[3, 3
+ d],x_p1=[2, 2 + d], x_p2=[2, 2 - d] , d
[d,d], force=[400, 0])
self.CD_force = ColumnDataSource(data=self.source_force)
plot.line('x', 'y', source=self.CD_force, color="Blue")
plot.line('x_p1','y_end_p', source=self.CD_force, color="red")
plot.line('x_p2', 'y_end_p', source=self.CD_force, color="green")
Force.list_force.append(self)
print("Kraft wurde erstellt")
def update_position(sefl, attr, old, new):
N = new
force = Force.list_force[0]
s = slice(2)
force.CD_force.data['x'] = 2 * [N]
force.CD_force.patch({'x': [(s, [N, N])]})
Force.list_force[0] = force
Kraft = Force()
slider_pos = Slider(title="Positoin", start=2, end=8, value=0, step =0.1)
slider_pos.on_change('value', Kraft.update_position)
layout = (column(slider_pos, plot))
show(layout)
你能不能创建一个简单的例子以便于复制?我在问题的末尾创建了一个简单的例子。正如我在那里添加的,我不确定更新是否因为我必须导入的不同类而受到影响。我已经解决了这个问题。您需要通过bokeh服务器运行此操作。在那之后,一切都很好。