简体   繁体   English

如何使用回调更改字形的颜色?

[英]How to change the color of a glyph using callback?

I've tried around using Bokeh, and now I want to search a word and change the color of its glyph.我已经尝试过使用 Bokeh,现在我想搜索一个单词并更改其字形的颜色。 My code looks like that:我的代码看起来像这样:

import bokeh.plotting as bp
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.io import vform

words = ["werner", "herbert", "klaus"]
x=[1,2,3]
y=[1,2,3]
color = ['green', 'blue', 'red']
word_input= TextInput(value="word", title="Point out a word")

source = bp.ColumnDataSource(data= dict(x=x,y=y,words=words, color='color'))
hover= HoverTool(tooltips=[("word", "@words")])

# output to static HTML file (with CDN resources)
bp.output_file("plot.html", mode="cdn")

# create a new plot with the tools above, and explicit ranges
p = bp.figure(plot_height = 600, plot_width = 800, title="word2vec", tools=[hover], logo =None)
# add a circle renderer with vectorized colors and sizes
p.circle('x','y', radius= 0.1, color = color, source=source, line_color=None)
callback= CustomJS(args=dict(source=source), code ="""
    var data = source.get('data');
    var glyph = cb_obj.get('value')
    words = data['words']
    colors=data['color']
    for (i=0; i< words.length;i++){
        if(glyph==words[i]){colors[i]='yellow'}        
    }
    source.trigger('change');
""")
layout = vform(word_input, p)
# show the results
bp.show(layout)

This code just doesn't work, and I can't figure out why not.这段代码行不通,我不知道为什么不行。

What am I doing wrong?我做错了什么? I've posted an other question earlier that day and this is kind of a first step solving it.那天早些时候我发布了另一个问题,这是解决它的第一步。

there are a couple of problems you have:你有几个问题:

  • you create the CallbackJS but never set is as the callback property of the TextInput您创建了CallbackJS但从未设置为TextInput的回调属性

  • you set the color key of data dict to the string "color" not to the list of colors您将数据字典的color键设置为字符串"color"而不是颜色列表

  • you passed the actual list of colors as the color argument to figure (it should be the string name of the data source column you want to use, eg "color" )您将实际的颜色列表作为color参数传递给figure (它应该是您要使用的数据源列的字符串名称,例如"color"


Here is a version that works:这是一个有效的版本:

from bokeh.io import vform
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.plotting import output_file, figure, show, ColumnDataSource

output_file("plot.html")

words = ["werner", "herbert", "klaus"]
x, y = [1,2,3], [1,2,3]
color = ['green', 'blue', 'red']

source = ColumnDataSource(data=dict(x=x, y=y, words=words, color=color))

hover = HoverTool(tooltips=[("word", "@words")])

p = figure(plot_height=600, plot_width=800, title="word2vec", tools=[hover])
p.circle('x','y', radius=0.1, fill_color='color', source=source, line_color=None)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.get('data')
    var value = cb_obj.get('value')
    var words = data['words']
    for (i=0; i < words.length; i++) {
        if ( words[i]==value ) { data.color[i]='yellow' }
    }
    source.trigger('change')
""")

word_input = TextInput(value="word", title="Point out a word", callback=callback)

layout = vform(word_input, p)

show(layout)

Updated @bigreddot's solution to work under Bokeh 2.2.3 if others come along similar tasks.更新@bigreddot 的解决方案,以便在其他人遇到类似任务时在 Bokeh 2.2.3 下工作。

from bokeh.layouts import column
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.plotting import output_file, figure, show, ColumnDataSource

output_file("plot.html")

words = ["werner", "herbert", "klaus"]
x, y = [1,2,3], [1,2,3]
color = ['green', 'blue', 'red']

source = ColumnDataSource(data=dict(x=x, y=y, words=words, color=color))

hover = HoverTool(tooltips=[("word", "@words")])

p = figure(plot_height=600, plot_width=800, title="word2vec", tools=[hover])
p.circle('x','y', radius=0.1, fill_color='color', source=source, line_color=None)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.data;
    var value = cb_obj.value;
    var words = data['words'];
    for (var i=0; i < words.length; i++) {
        if ( words[i]==value ) { data.color[i]='yellow' }
    }
    source.change.emit();
""")

word_input = TextInput(value="word", title="Point out a word")

word_input.js_on_change('value', callback)

layout = column(word_input, p)

show(layout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM