Skip to main content
 首页 » 编程设计

python之有没有办法在 Bokeh 中为非选择字形设置多个不透明度

2025年05月04日16think

我有两个链接的散点图,其中的点具有不同的不透明度。问题在于,当使用框选工具选择某些点时,所有未选择的点都变成相同的不透明度。

我希望未选中的点保持原来的不透明度。可以链接具有不同不透明度的点,因此我无法通过为每个不透明度值制作一个点数组来解决问题。

有没有一种方法可以在绘图 API 中实现这一点?

我能否扩展 nonselection_glyph,使其 alpha 属性像圆形标记的 alpha 属性一样接受不透明度值数组?

import numpy as np 
from bokeh.plotting import output_file, figure, gridplot, show 
from bokeh.models import ColumnDataSource, Circle 
 
N = 100 
max = 100 
x1 = np.random.random(size = N) * max 
y1 = np.random.random(size = N) * max 
a1 = np.random.choice(a = [0.2, 0.5, 1], size = N) 
x2 = np.random.random(size = N) * max 
y2 = np.random.random(size = N) * max 
a2 = np.random.choice(a = [0.2, 0.5, 1], size = N) 
 
output_file('scatter.html') 
source = ColumnDataSource(data = dict(x1 = x1, y1 = y1, x2 = x2, y2 = y2, 
                                      a1 = a1, a2 = a2)) 
 
left = figure(tools = 'box_select, tap', width = 400, height = 400, 
              x_range = (0,100), y_range = (0,100)) 
right = figure(tools = 'box_select, tap', width = 400, height = 400, 
               x_range = (0,100), y_range = (0,100)) 
 
points1 = left.circle('x1', 'y1', source = source, size = 10, 
                      fill_color = 'blue', line_color = None, alpha = 'a1') 
points2 = right.circle('x2', 'y2', source = source, size = 10, 
                       fill_color = 'blue', line_color = None, alpha = 'a2') 
points1.selection_glyph = Circle(fill_color = 'red', line_color = None) 
points2.selection_glyph = Circle(fill_color = 'red', line_color = None) 
 
p = gridplot([[left, right]]) 
show(p) 

请您参考如下方法:

试试这个:

import numpy as np 
from bokeh.plotting import output_file, figure, gridplot, show 
from bokeh.models import ColumnDataSource, Circle 
 
N = 100 
max = 100 
x1 = np.random.random(size = N) * max 
y1 = np.random.random(size = N) * max 
a1 = np.random.choice(a = [0.2, 0.5, 1], size = N) 
x2 = np.random.random(size = N) * max 
y2 = np.random.random(size = N) * max 
a2 = np.random.choice(a = [0.2, 0.5, 1], size = N) 
 
source = ColumnDataSource(data = dict(x1 = x1, y1 = y1, x2 = x2, y2 = y2, 
                                      a1 = a1, a2 = a2, a1n = a1 * 0.5, a2n=a2*0.5)) 
 
left = figure(tools = 'box_select, tap', width = 400, height = 400, 
              x_range = (0,100), y_range = (0,100)) 
right = figure(tools = 'box_select, tap', width = 400, height = 400, 
               x_range = (0,100), y_range = (0,100)) 
 
points1 = left.circle('x1', 'y1', source = source, size = 10, 
                      fill_color = 'blue', line_color = None, alpha = 'a1') 
points2 = right.circle('x2', 'y2', source = source, size = 10, 
                       fill_color = 'blue', line_color = None, alpha = 'a2') 
points1.selection_glyph = Circle(fill_color = 'red', line_color = None) 
points2.selection_glyph = Circle(fill_color = 'red', line_color = None) 
points1.nonselection_glyph.fill_alpha = "a1n" 
points2.nonselection_glyph.fill_alpha = "a2n" 
p = gridplot([[left, right]]) 
show(p)