首页 > 解决方案 > 散景使用滑块更新列中的特定列表

问题描述

我必须显示具有不同轴承和不同位置的力的梁中的内力。为了将力移动到不同的位置,我想使用on_change带有滑块的散景功能。为了做到这一点,我需要更改ColumnDataSource班级中我的一个列表的值Froce。到目前为止,我可以创建和绘制所有内容,但是当我更改滑块的值时,它不会更改列表的值,也不会更改我的力在图中的位置。

这是 cals Force 的构造函数

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])]})

我曾尝试更新 theColumnData以及 the ,dict但这些都没有奏效。

这是我创建所有数字、滑块、排列布局和回调函数的绘图部分

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)

我试图将其分解为这个基本代码,我想在其中更改我创建的箭头的位置。我不确定update-funkction在某些时候是否由于不同工作表中的不同类而受到影响。如果不是这总结了我在更新时遇到的问题。

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)

标签: pythonbokeh

解决方案


推荐阅读