我已经检查了许多类似问题的答案,但没有一个是相关的。对我来说,悬停工具已启用,但无论我在多大程度上将项目与其他项目隔离(不重叠),工具提示都不会显示。现在我使用段字形但是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)段悬停肯定存在一个问题,它不适用于反转轴(在这种情况下为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
所以$x0,$x1,$y0,$y1
不被HoverTool理解,不能使用。我用@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)
结果: