Skip to main content
 首页 » 编程设计

python之更改未选定 Bokeh 线的颜色

2024年10月24日12mengfanrong

我开始使用 Bokeh 绘制不共享公共(public) x 或 y 变量的数据。我希望能够选择一条线并让其他未选择的线变灰。理想情况下,选定的线也将被带到绘图的前面。

到目前为止,我已经能够选中该行,但是我找不到一种方法可以使未选中的行“变灰”,或者设置选中行的级别。

import numpy as np 
from bokeh.plotting import figure, show, output_file 
from bokeh.models.sources import ColumnDataSource 
from bokeh.models import Line,TapTool 
 
output_file("test.html") 
 
x0s = np.random.randint(0,20,20) 
y0s = np.random.randint(0,20,20) 
x1s = np.random.randint(0,20,20) 
y1s = np.random.randint(0,20,20) 
 
p_left = figure(tools=[TapTool()]) 
 
for xs,ys in zip([x0s,x1s],[y0s,y1s]): 
    source = ColumnDataSource({'x': xs, 'y': ys}) 
    default_line = Line(x='x', y='y', line_color='blue', line_width=2) 
    selected_line = Line(line_color='red', line_width=4) 
    nonselected_line = Line(line_color='grey') 
    p_left.add_glyph(source,default_line,selection_glyph=selected_line,nonselection_glyph=nonselected_line) 
 
show(p_left) 

请您参考如下方法:

我遇到了类似的情况,找到了这个例子:

https://docs.bokeh.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs

我自己还没有尝试过,但似乎接近您要找的东西。

编辑 刚试过,对我来说完美无缺。