首页 > 解决方案 > 未提供 y_range.start 时,散景悬停不适用于零值

问题描述

当未提供 y_range.start 时,散景悬停不适用于值零,例如,在以下代码的情况下,当我们将鼠标悬停在油桃上时,悬停不会显示计数“0”。

    from bokeh.io import show, output_file
    from bokeh.models import ColumnDataSource
    from bokeh.palettes import Spectral6
    from bokeh.plotting import figure
    from bokeh.transform import factor_cmap
    output_file("colormapped_bars.html")
    fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
    counts = [5, 3, 0, 2, 5, 6]
    data = {'fruits' : fruits, 'counts'   : counts}
    source = ColumnDataSource(data=dict(fruits=fruits, counts=counts))
    p = figure(x_range=fruits, plot_height=250, toolbar_location=None, title="Fruit Counts", tools="hover", tooltips="Count: @counts")
    p.vbar(x='fruits', top='counts', width=0.9, source=source, legend="fruits", line_color='white', fill_color=factor_cmap('fruits', palette=Spectral6, factors=fruits))
    p.xgrid.grid_line_color = None

    show(p)

但是如果我添加p.y_range.start = 0,悬停就可以了。当存在负范围时,悬停也不起作用,例如p.y_range.start = -5

请帮助我在这里做错了什么。

提前致谢。

标签: python-3.xbokeh

解决方案


我不确定你在期待什么。字形的默认“点”悬停基于屏幕上的存在(即实际占用的像素空间)。您的“油桃”栏的高度为零,因此不占用屏幕空间。就悬停的“点”命中测试而言(默认模式),它或多或少是一个不可见的目标。(如果它在任何情况下“有效”,由于舍入问题几乎可以肯定是偶然的。)

如果您希望在光标位于条形类别上方的任何位置时弹出悬停,您可以设置:

p.hover.mode = "vline"

这是一种不同的悬停模式,甚至可以“看到”零高度条

在此处输入图像描述


推荐阅读