有没有办法让Bokeh中的选择字形有多种颜色?

时间:2015-12-18 19:52:05

标签: python bokeh

我有一个散点图,其中x值<1的点。 50是蓝色,并且具有x值> 0的点。 50是红色的。当我用方框选择工具选择颜色时,我试图将颜色反转。选定的蓝色应变为红色,反之亦然。

我尝试通过为fill_color属性的selection_glyph属性提供一组颜色来实现此目的,但该属性不会采用数组。还有另一种方法可以实现这个目标吗?

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)

1 个答案:

答案 0 :(得分:3)

是。将您的数据拆分为两组,每组都有自己的p.circle调用,为每个调用提供不同的选择/非选择策略:

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)

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