有没有办法在 Bokeh 中为选择字形设置多种颜色?

Is there a way to have multiple colours for selection glyphs in Bokeh?

我有一个散点图,其中 x 值 < 50 的点为蓝色,x 值 > 50 的点为红色。当我使用方框 select 工具 select 时,我试图让颜色反转。蓝色的 selected 颜色应该变成红色,反之亦然。

我试图通过为 selection_glyph 属性 的 fill_color 属性提供一个颜色数组来做到这一点,但是 属性 不采用数组.还有其他方法可以实现吗?

import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import Circle

N = 100
max = 100
x = np.random.random(size=N) * max
y = np.random.random(size=N) * max
output_file("scatter.html")

color1 = []
color2 = []
for a in x:
    if a > 50:
        color1.append("red")
        color2.append("blue")
    else:
        color1.append("blue")
        color2.append("red")

p = figure(tools = "box_select, tap", width = 400, height = 400, 
           x_range = (0,100), y_range = (0,100))

circles = p.circle(x, y, size=10, fill_color = color1, line_color = None)
#circles.selection_glyph = Circle(fill_color = color2, line_color = None)
#circles.nonselection_glyph = Circle(fill_color = color1, line_color = None)

show(p)

是的。将您的数据分成两组,用它们自己对 p.circle 的调用绘制每组,为每个调用提供不同的 selection/nonselection 策略:

p = figure(tools = "box_select, tap", width = 400, height = 400, 
           x_range = (0,100), y_range = (0,100))

circles1 = p.circle(x1, y1, size=10, color="red", line_color=None)
circles1.selection_glyph    = Circle(fill_color="blue", line_color=None)
circles1.nonselection_glyph = Circle(fill_color="red",  line_color=None)

circles2 = p.circle(x2, y2, size=10, color="blue", line_color=None)
circles2.selection_glyph    = Circle(fill_color="blue", line_color=None)
circles2.nonselection_glyph = Circle(fill_color="red",  line_color=None)

作为奖励,您不必为每个散点发送一长串颜色(以防您有很多点)。