问题描述
我正在使用Bokeh通过将 ColumnDataSource
传递给 figure.circle
函数来创建散点图。数据源具有为每个点指定某些颜色的列,每行都有一个十六进制代码,因为我想要使用的着色方案有点复杂。
I'm using Bokeh to create scatter plots by passing a ColumnDataSource
to the figure.circle
function. The data source has columns that designate certain colors for each point, with a hex code in each row, because the coloring scheme I want to use is somewhat complicated.
是否存在一种更改用于为小部件回调中的圆圈着色的列的方法?我想象一个下拉菜单,允许用户为点选择各种着色方案。
Is there a way to change the column used to color the circles in the callback of a widget? I'm imagining a dropdown menu allowing users to choose various coloring schemes for the points.
推荐答案
这是一个解决方案的例子使用 models.Select
小部件和 models.CustomJS
选择<$ c $中定义的两种着色方案c> ColumnDataSource of Figure.circle
:
Here is an example of a solution using a models.Select
widget and models.CustomJS
to select out of two coloring schemes defined in the ColumnDataSource
of Figure.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)
输出看起来像
The output looks like
这篇关于Bokeh,如何使用CustomJS回调更改用于字形颜色的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!