与段字形关联时,悬停工具不显示工具提示

时间:2019-03-14 16:06:32

标签: python bokeh segment

我检查了许多类似问题的答案,但都没有相关的答案。对我而言,启用了悬停工具,但是无论我进行多大缩放以使该项目与所有其他项目(没有重叠)都隔离开,该工具提示永远不会显示。现在,我正在使用段字形在另一个应用程序上工作,但line_width = 12(x)并随着段高度的缩放扩大(y)并最终显示工具提示。在不起作用的地方,line_width = 1(大量数据),该线段是一条水平线(y0,y1的y值相同)。我认为这可能与line_width太小有关。但是,即使将其值设置为非常大(20)并放大并消除所有重叠,也不会显示工具提示。 这是我的测试代码:

from bokeh.io import output_file
from bokeh.models.ranges import Range1d
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.models.tools import HoverTool
import random

def build_test_data(dataSeries2gen, count2gen, series2gen):
    # test data
    vectorSeries = []
    if dataSeries2gen == "horizontal":
        # gen random seed numbers for X and Y but always use same Y
        for nSeries in range(0, series2gen):
            sameY = [random.randint(1, 4000) for y in range(4000)]
            randX1 = [random.randint(1, 51200) for x in range(51200)]
            randX2 = [random.randint(1, 51200) for x in range(51200)]
            nextSeries = []
            for nextSeed in range(1, count2gen):
                yIdx = random.randint(1, 3999)
                xIdx = random.randint(1, 51999)
                nextSegment = [randX1[xIdx], sameY[yIdx],\ 
                               randX2[xIdx], sameY[yIdx]]
                nextSeries.append(nextSegment)
            vectorSeries.append(nextSeries)
    else:
        for nSeries in range(0, series2gen):
            randY1 = [random.randint(1, 4000) for y in range(4000)]
            randY2 = [random.randint(1, 4000) for y in range(4000)]
            randX1 = [random.randint(1, 51200) for x in range(51200)]
            randX2 = [random.randint(1, 51200) for x in range(51200)]
            nextSeries = []
            for nextSeed in range(1, count2gen):
                nextSegment = [randX1[nextSeed], randY1[nextSeed],\
                              [randX2[nextSeed], randY2[nextSeed]]
                nextSeries.append(nextSegment)
            vectorSeries.append(nextSeries)

    plot_segments_withSource(vectorSeries)

def plot_segments_withSource(theData):

    colorPalette = ["red", "green", "blue", "yellow", "orange"]

    output_file("test.html", mode='inline')
    xRange = Range1d(1, 51200)
    yRange = Range1d(1, 4000)

    p = figure(plot_width=700, plot_height=750, x_range=xRange,\
               y_range=yRange, output_backend="webgl")
    colorIdx = -1
    for nSeries in theData:
        colorIdx += 1
        color2use = colorPalette[colorIdx]
        x0 = []
        y0 = []
        x1 = []
        y1 = []
        for vPoints in nSeries:
            x0.append(vPoints[0])
            y0.append(vPoints[1])
            x1.append(vPoints[2])
            y1.append(vPoints[3])
        source = ColumnDataSource(dict(
            x0 = x0,
            y0 = y0,
            x1 = x1,
            y1 = y1
            )
        )
        p.segment(x0="01", y0="y0", x1="x1", y1="y1",\
                  line_color=color2use, source=source, line_width=1)

    # invert yAxis where 0 is at the top
    p.y_range = Range1d(4000, 0)

    hover = HoverTool()
    hover.tooltips = [("x0,y0", "$x0,$y0"),
                      ("x1,y1", "$x1,$y1")]
    hover.point_policy = "snap_to_data"
    hover.line_policy = "nearest"

    p.add_tools(hover)

    p.title.text = "This is a test"
    p.legend.location = "top_right"
    p.xaxis.axis_label = "Occurrences"
    p.yaxis.axis_label = "Time"

    show(p)

if __name__ == "__main__":
    dataSeries2gen = "horizontal"
    count2gen = 100000
    series2gen = 2
    build_test_data(dataSeries2gen, count2gen, series2gen)

我需要这样做才能同时显示数据和设置自定义JavaScript(鼠标单击事件)以执行其他操作。但是,如果悬停工具无法识别项目是什么,则单击事件也将不会触发。

谢谢

1 个答案:

答案 0 :(得分:1)

我在您的代码中发现了2个问题:

1)分段悬停肯定存在一个问题,该问题在反向轴(在这种情况下为Y轴)上不起作用。这可能是散景问题(请检查/提交问题)

2)工具提示语法不正确。根据Bokeh文档,仅允许使用以下语法

:$index: index of hovered point in the data source
:$name: value of the ``name`` property of the hovered glyph renderer
:$x: x-coordinate under the cursor in data space
:$y: y-coordinate under the cursor in data space
:$sx: x-coordinate under the cursor in screen (canvas) space
:$sy: y-coordinate under the cursor in screen (canvas) space
:$color: color data from data source

HoverTool无法理解$x0,$x1,$y0,$y1,因此无法使用。我将它们替换为@x0,@y0,@x1,@y1,它们等效于$x,$y,以防您的光标指向某个段的开头或结尾。我还添加了("x,y", "$sx,$sy")以显示光标下方的屏幕坐标。

要正确显示悬停工具提示,我做了一个技巧,隐藏了原始的y轴并插入了一个额外的倒置轴。这是可行的,但实际上没有意义,因为轴是倒置的,但y范围却没有。无论如何,我希望这会有所帮助。

from bokeh.io import output_file
from bokeh.models.ranges import Range1d
from bokeh.models import ColumnDataSource, LinearAxis
from bokeh.plotting import figure, show
from bokeh.models.tools import HoverTool
import random

def build_test_data(dataSeries2gen, count2gen, series2gen):
    # test data
    vectorSeries = []
    if dataSeries2gen == "horizontal":
        # gen random seed numbers for X and Y but always use same Y
        for nSeries in range(0, series2gen):
            sameY = [random.randint(1, 4000) for y in range(4000)]
            randX1 = [random.randint(1, 51200) for x in range(51200)]
            randX2 = [random.randint(1, 51200) for x in range(51200)]
            nextSeries = []
            for nextSeed in range(1, count2gen):
                yIdx = random.randint(1, 3999)
                xIdx = random.randint(1, 51199)
                nextSegment = [randX1[xIdx], sameY[yIdx], randX2[xIdx], sameY[yIdx]]
                nextSeries.append(nextSegment)
            vectorSeries.append(nextSeries)
    else:
        for nSeries in range(0, series2gen):
            randY1 = [random.randint(1, 4000) for y in range(4000)]
            randY2 = [random.randint(1, 4000) for y in range(4000)]
            randX1 = [random.randint(1, 51200) for x in range(51200)]
            randX2 = [random.randint(1, 51200) for x in range(51200)]
            nextSeries = []
            for nextSeed in range(1, count2gen):
                nextSegment = [randX1[nextSeed], randY1[nextSeed], randX2[nextSeed], randY2[nextSeed]]
                nextSeries.append(nextSegment)
            vectorSeries.append(nextSeries)
    plot_segments_withSource(vectorSeries)

def plot_segments_withSource(theData):
    colorPalette = ["red", "green", "blue", "yellow", "orange"]
    output_file("test.html", mode = 'inline')
    p = figure(plot_width = 700, plot_height = 750, output_backend = "webgl", y_axis_location = None)
    colorIdx = -1
    for nSeries in theData:
        colorIdx += 1
        color2use = colorPalette[colorIdx]
        x0, y0, x1, y1 = [], [], [], []
        for vPoints in nSeries:
            x0.append(vPoints[0])
            y0.append(vPoints[1])
            x1.append(vPoints[2])
            y1.append(vPoints[3])
        source = ColumnDataSource(dict(x0 = x0,y0 = y0,x1 = x1,y1 = y1))
        p.segment(x0 = "x0", y0 = "y0", x1 = "x1", y1 = "y1", line_color = color2use, source = source, line_width = 1)

    # insert inverted extra yAxis where 0 is at the top
    p.extra_y_ranges = {"Time Extra Axis": Range1d(start = 4000, end = 0) }
    p.add_layout(LinearAxis(y_range_name = "Time Extra Axis", axis_label = 'Time'), 'left')
    hover = HoverTool()
    hover.tooltips = [("x0,y0", "@x0,@y0"), ("x1,y1", "@x1,@y1"), ("x,y", "$sx,$sy")]
    hover.point_policy = "snap_to_data"
    hover.line_policy = "nearest"
    p.add_tools(hover)
    p.title.text = "This is a test"
    p.xaxis.axis_label = "Occurrences"
    p.yaxis.axis_label = "Time"
    show(p)

if __name__ == "__main__":
    dataSeries2gen = "horizontal"
    count2gen = 10
    series2gen = 2
    build_test_data(dataSeries2gen, count2gen, series2gen)

结果:

enter image description here