使用 Bokeh 绘图时如何为多个数据集之一添加 hovertool

How to add hovertool for just one of multiple datasets when plotting using Bokeh

我正在 Python 中使用 Bokeh 绘制两个数据集。我将一个数据集绘制为一条线,将另一个数据集绘制为一个标记(圆形十字)。我正在尝试为标记数据集添加一个 hovertool。

source = ColumnDataSource(df_bcn_rns)

p = figure(plot_height=300,
           plot_width=800,
           tools="",
           toolbar_location=None,
           x_axis_type='datetime',
           x_axis_location="above",
           background_fill_color="#efefef") #,x_range=(bcn_sp.index[-1], bcn_sp.index[0])

p.line(x=bcn_sp['Date'],
       y=bcn_sp['Close'])
#,source=source)

p.circle_cross(x='Date',
               y='price',
               source=source)

p.yaxis.axis_label = 'Closing Price'


p.add_tools(HoverTool(
        tooltips=[( 'date',   '@Date{%d-%m-%Y}'),
                  ( 'price',  '@price{00.2f}p'), # use @{ } for field names with spaces
                  ( 'rns header', '@Headline')],

        formatters={'Date' : 'datetime', # use 'datetime' formatter for 'date' field
                    'close' : 'printf'},   # use 'printf' formatter for 'adj close' field
        mode='vline' # display a tooltip whenever the cursor is vertically in line with a glyph
    ))

show(column(p)) #,select

上面的代码有效,但是,悬停工具会在鼠标悬停在任一数据集上时显示。当鼠标悬停在线数据集上时,显示“???”在悬停框中,我不希望它显示任何内容。

总而言之,我期待的结果是当鼠标悬停在标记上时会显示悬停框,但当鼠标悬停在直线上时不会显示悬停框,因为此数据集未链接到 "source" .有什么解决办法的建议吗?

markers = p.circle_cross(...) returns 您需要指定为 HoverTool 的唯一渲染器的 markers 渲染器。查看两个可能的选项如何操作 here