Python 与线段图示符关联时,“悬停工具”不显示工具提示
我检查了许多类似问题的答案,但没有一个是相关的。对我来说,悬停工具是启用的,但无论我放大多远以将项目与所有其他项目隔离(无重叠),工具提示都不会显示。现在,我在另一个应用程序上使用了线段glyph,但线宽=12(x),当一个应用程序放大线段的高度时,会展开(y),最终会显示工具提示。当它不工作时,线宽=1(大量数据),该段为水平线(y值与y0、y1相同)。我想这可能与线宽太小有关。但即使将其值设置为非常大(20)并放大并消除所有重叠,也不会显示工具提示。 以下是我的测试代码:Python 与线段图示符关联时,“悬停工具”不显示工具提示,python,bokeh,segment,Python,Bokeh,Segment,我检查了许多类似问题的答案,但没有一个是相关的。对我来说,悬停工具是启用的,但无论我放大多远以将项目与所有其他项目隔离(无重叠),工具提示都不会显示。现在,我在另一个应用程序上使用了线段glyph,但线宽=12(x),当一个应用程序放大线段的高度时,会展开(y),最终会显示工具提示。当它不工作时,线宽=1(大量数据),该段为水平线(y值与y0、y1相同)。我想这可能与线宽太小有关。但即使将其值设置为非常大(20)并放大并消除所有重叠,也不会显示工具提示。 以下是我的测试代码: from boke
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(鼠标点击事件)来做其他事情。但是,如果悬停工具没有识别出项目是什么,那么click事件也不太可能触发
谢谢我在您的代码中发现了两个问题: 1) 分段悬停在反转轴(本例中为Y轴)上不起作用肯定存在问题。这可能是Bokeh问题(请检查/提交问题) 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轴。这是可行的,但实际上没有意义,因为轴是反转的,但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)
结果:
发现GitHub问题#6704:水平/垂直段上的修复悬停命中测试#6930在0.12.10版中得到解决。我的bokeh版本是0.13.0您的权利,谢谢。然而,这是一个bug吗?我需要反转Y轴。还有别的工作吗。我还要补充一点,在我的另一个应用程序中,也有一个倒置的Y轴,它可以工作。也许有一个工具提示语法的解决办法?我在回答中粘贴了一个修改过的代码。你能确认它对你有用吗?托尼,谢谢你的更新。但否,设置“yRange=DateRange1d(4000,0)”不会反转范围。即使反转值(0,4000),零(0)仍位于绘图的底部。它确实使用“yRange=[4000,0]”反转,但悬停工具没有显示工具提示。您是否更新到Bokeh v1.0.4?上图显示了反转的范围和显示画布x+y的工具提示