Bokeh,如何使用CustomJS回调更改用于字形颜色的列?

qsf*_*fzy 3 javascript python bokeh

我正在使用Bokeh通过传递ColumnDataSourcefigure.circle函数来创建散点图.数据源具有为每个点指定某些颜色的列,每行都有一个十六进制代码,因为我想要使用的着色方案有点复杂.

有没有办法在窗口小部件的回调中更改用于为圆圈着色的列?我想象一个下拉菜单,允许用户为点选择各种着色方案.

Pab*_*yes 7

下面是一个使用的溶液的例子models.Select插件和models.CustomJS选择出在定义的两个着色方案ColumnDataSourceFigure.circle:

import bokeh
import bokeh.plotting
p = bokeh.plotting.figure(x_range=(0,4), y_range=(0,4), plot_height=200 )
csource = bokeh.models.ColumnDataSource(data=dict(
        x=[1,2,3],
        y=[1,2,1],
        colors1=["#ff0000","#00ff00","#0000ff"],
        colors2=["#ff00ff","#ffff00","#00ffff"]))
cir = p.circle(x="x",y="y",fill_color="colors1",line_color="colors1",
               size=20,source=csource)
cb_cselect = bokeh.models.CustomJS(args=dict(cir=cir,csource=csource), code ="""
    var selected_color = cb_obj.value;
    cir.glyph.line_color.field = selected_color;
    cir.glyph.fill_color.field = selected_color;
    csource.trigger("change")
""")
color_select = bokeh.models.Select(title="Select colors", value="colors1", 
                    options = ["colors1","colors2"], callback = cb_cselect)
layout = bokeh.layouts.gridplot([[p],[color_select]])
bokeh.io.output_file("output.html")
bokeh.io.show(layout)
Run Code Online (Sandbox Code Playgroud)

输出看起来像 在此输入图像描述