python - Bokeh - 点击时显示相应的文本

标签 python bokeh

当用户单击第一个图上的圆圈时,我想显示简单的文本(在第二个图上)。 我盯着 bokeh 网站上显示第一个图的工作示例。

我在下面做了一些更改,以便当用户单击第一个图上的圆圈时,第二个图应显示相应的文本。 因此,当用户单击第一个图上的 (x = 1, y =2) 上的圆圈时,第二个图上显示的文本应为 ('first')

使用下面的代码,我根本看不到任何情节,也没有错误!我知道如何调试这个问题

我也愿意接受不同的方法

Plot of Circles

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Plot, Range1d, Text
from bokeh.layouts import column

output_file("styling_selections.html")

source = ColumnDataSource(data=dict(x = [1,2,3],
                                    y = [2,5,3],
                                    name = ['first', 'second', 'third'],
                                    ))


plot = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle")
renderer = plot.circle(x= 'x', y= 'y', size=50,
                       # set visual properties for selected glyphs
                       selection_color="firebrick",    
                       # set visual properties for non-selected glyphs
                       nonselection_fill_alpha=0.2,
                       nonselection_fill_color="blue",
                       nonselection_line_color="firebrick",
                       nonselection_line_alpha=1.0,
                       source = source)

def construct_text_box(source): 
    # Plot and axes                                                             
    xdr = Range1d(0, 220)                                                       
    ydr = Range1d(0, 120)                                                       

    plot = Plot(                                                                
        x_range=xdr,                                                            
        y_range=ydr,                                                            
        #title="",                                                               
        plot_width=250,                                                         
        plot_height=120,                                                        
        min_border=0,                                                           
    )                                                                           
    # Add the writing                                                           
    country = Text(x=5, y=50, text='cpname', text_font_size='18pt', text_color="#FFFFF", text_font_style="bold")                    
    plot.add_glyph(source, Text(), selection_glyph=country)                     

    return plot

text_box = construct_text_box(source)
show(column(plot,text_box))

最佳答案

您的代码中存在一些错误,导致绘图无法显示。调整以下内容至少会显示两个图。然而,第二个图将只是您想要的文本相互叠加。为了获得您正在寻找的功能,我猜您需要使用回调:

  # changed text parameter from cpname to name, thats the column you use
  # FFFFF was white, you wouldn't see this, changed to black
    country = Text(x=5, y=50, text='name', text_color="#000000")
    plot.add_glyph(source, country)

如果我有时间,我稍后会研究回调部分。但这至少应该让您朝着正确的方向开始。

/e:这是一个工作示例。正如之前所怀疑的,添加回调会添加您想要的功能。棘手的部分是 JavaScript 代码。我希望评论足以理解它:

from bokeh.io import output_file, show, output_notebook, reset_output
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Plot, Range1d, Text, TapTool, CustomJS
from bokeh.layouts import column

reset_output()

source = ColumnDataSource(data=dict(x = [1, 10, 16], y = [2, 20, 10],
                                    name_labels = ['first', 'second', 'third'],
                                    name_display = ['','','']
                                    ))

plot = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle")
renderer = plot.circle(x= 'x', y= 'y', size=50,
                       # set visual properties for selected glyphs
                       selection_color="firebrick",    
                       # set visual properties for non-selected glyphs
                       nonselection_fill_alpha=0.2, nonselection_fill_color="blue",
                       nonselection_line_color="firebrick", nonselection_line_alpha=1.0,
                       source = source)

def construct_text_box(source): 
    # Plot and axes                                                             
    xdr = Range1d(0, 220)                                                       
    ydr = Range1d(0, 120)                                                       

    plot = Plot(                                                                
        x_range=xdr, y_range=ydr,                                                        
        plot_height=120, min_border=0,                                                           
    )                                                                           
    # Add the writing                                                           
    country = Text(x=5, y=50, text='name_display', text_color="#000000")
    plot.add_glyph(source, country)    
    return plot

output_notebook()

JS_CODE ="""
    // Get index of current selected datapoint
    sel_point_index = cb_data.source.attributes.selected["1d"]["indices"][0];     

    /* replace all name_display values with the name_label
       value of currently selected point */
    for (i=0; i < cb_data.source.data.name_labels.length; i++) {
        cb_data.source.data.name_display[i] = cb_data.source.data.name_labels[sel_point_index];
        }       
    cb_data.source.change.emit();
    """

newtaptool = plot.select(type=TapTool)
newtaptool.callback = CustomJS(code=JS_CODE)

text_box = construct_text_box(source)
show(column(plot, text_box))

关于python - Bokeh - 点击时显示相应的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803577/

相关文章:

python - 如何检查列是否包含 pandas 中的字符串

python - 合并(更新\插入)pandas 数据帧的更好方法

python - Bokeh 多线使一条线不可见

python - 使用 Bokeh 绘图 x 轴转换 Unix 时间戳

python - Bokeh 悬停工具提示不显示所有数据 - Ipython 笔记本

python - "reset" Bokeh 图的命令是什么?

python - 获取 bokeh 应用程序的 URL 参数

Python,在 excel 工作表中搜索列中的特定字符串并将这些行提取到文本文件

python - “bytes”对象不能被解释为整数

python - 如何编写用于下载的python脚本?