使用HoverTool()工具时仅显示一个工具提示 [英] Displaying only one tooltip when using the HoverTool() tool

查看:225
本文介绍了使用HoverTool()工具时仅显示一个工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bokeh中绘制了很多点,并且添加了

I am plotting very many points in Bokeh, and I have added the HoverTool to the list of tools of the figure, so that the mouse shows the x,y coordinates of the mouse when close to a glyph.

当鼠标靠近紧密堆积在一起的一组字形时,我得到的提示与字形一样多.我只需要一个工具提示,即最接近的字形之一.这不仅是演示文稿的细节,因为在很多方面都会得到以下结果:

When the mouse gets close to a set of glyphs closely packed together, I get as many tooltips as glyphs. I want instead only one tooltip, the one of the closest glyph. This isn't just a presentation detail, because for very many points this results:

  • 与绘图的交互缓慢,在生成所有工具提示时浏览器会卡住
  • 在很长的工具提示中,相同的信息被重复的次数是靠近游标的字形的次数

下面是一个示例,其中包含用于复制行为的代码:

An example follows, with the code to replicate the behaviour:

import numpy.random
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import HoverTool
output_notebook()

hover = HoverTool()
hover.tooltips = [("(x,y)", "($x, $y)")]

x = numpy.random.randn(500)
y = numpy.random.randn(500)

p = figure(tools=[hover])
p.circle(x,y, color='red', size=14, alpha=0.4)

show(p)

推荐答案

我遇到了类似的问题,并提出了使用自定义工具提示的解决方案.我在顶部插入一个样式标签,该标签仅显示.bk-tooltip类下的第一个子元素div,这是第一个工具提示.

I was having a similar problem and came up with a solution using a custom tooltip. I insert a style tag at the top that only displays the first child div under the .bk-tooltip class, which is the first tooltip.

这是一个可行的示例:

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

custom_hover = HoverTool()

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

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

p = figure(tools=[custom_hover]) #Custom behavior
#p = figure(tools=['hover'])  #Default 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)

这是一种骇人听闻的解决方案,但可以在Safari,Firefox和Chrome浏览器中使用.我认为他们会推出更长期的

This is kind of a hacky solution, but it works in Safari, Firefox and Chrome. I think they'll be coming out with a more long-term solution soon.

这篇关于使用HoverTool()工具时仅显示一个工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆