【问题标题】:Displaying only one tooltip when using the hovertool in Bokeh在 Bokeh 中使用 hovertool 时仅显示一个工具提示
【发布时间】:2021-07-24 19:20:13
【问题描述】:

这是我的第一个 stackoverflow 问题,所以我为重复的帖子道歉;我没有对original thread 发表评论的声誉,也不确定如何提出这个问题。

我有一个plot with a hover tooltip,它经常显示一长串返回值。如果有重叠的值,我只想显示一个(任何一个)。

Screenshot of bokeh plot with many hover tooltips

我已经尝试了original thread 上发布的几个 CSS 解决方案,但仍然会显示多个工具提示。我不确定我是否执行代码 sn-ps 错误,或者解决方案是否不再有效。

Screenshot of basic plot from the original post showing the undesired behavior of multiple hover tooltips

我正在使用散景 2.3.3 和 Chrome。基本代码示例如下,我正在尝试的最终项目是here

from bokeh.plotting import figure, show
from bokeh.models import HoverTool, Range1d

custom_hover = HoverTool()

custom_hover.tooltips = """
    <style>
        div.bk-tooltip.bk-right>div.bk>dif:not(:first-child) {
            display:none !important;
            }
        div.bk-tooltip.bk-left>div.bk>dif:not(:first-child) {
            display:none !important;
            }
    </style>


    <b>X: </b> @x <br>
    <b>Y: </b> @y
"""

p = figure(tools=[custom_hover]) #Custom behavior

p.circle(x=[0.75,0.75,1.25,1.25], y=[0.75,1.25,0.75,1.25], size=230, color='red', fill_alpha=0.2)
p.y_range = Range1d(0,2)
p.x_range = Range1d(0,2)

show(p)

感谢您的帮助!

【问题讨论】:

    标签: python css hover tooltip bokeh


    【解决方案1】:

    下面的解决方案只允许一个可见的工具提示。这可以通过修改参数num来改变。

    此解决方案已使用 Bokeh 2.3.2 进行测试。

    from bokeh.plotting import figure, show, output_notebook
    from bokeh.models import HoverTool, Range1d, CustomJSHover
    output_notebook()
    
    p = figure(tools='')
    p.circle(x=[0.75,0.75,1.25,1.25], y=[0.75,1.25,0.75,1.25], size=230, color='red', fill_alpha=0.2)
    p.y_range = p.x_range = Range1d(0,2)
    
    t = """
    <div @x{custom}>
        <b>X: </b> @x <br>
        <b>Y: </b> @y
    </div>
    """
    
    # define how many tooltips you want to show as maximum
    num = 1
    
    f = CustomJSHover(code=f"""
    special_vars.indices = special_vars.indices.slice(0,{num})
    return special_vars.indices.includes(special_vars.index) ? " " : " hidden "
    """)
    
    p.add_tools(HoverTool(tooltips=t, formatters={'@x': f}))
    
    show(p)
    

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 2021-09-27
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多