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

2024-06-25 22:31:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python中的Bokeh绘制两个数据集。一个数据集我绘制为一条线,另一个作为一个标记(圆圈交叉)。我正在尝试为标记数据集添加一个悬停工具

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

上面的代码可以工作,但是当鼠标停留在任一数据集上时,hovertool就会显示出来。当鼠标放在数据集行上时,它在悬停框中显示“?”,我不希望它显示任何内容

总而言之,我期望的结果是鼠标在标记上时显示悬停框,但当鼠标在行上时没有悬停框,因为这个数据集没有链接到“source”。有什么建议我可以解决这个问题吗


Tags: 数据标记fieldsourcefordatetimedateuse
1条回答
网友
1楼 · 发布于 2024-06-25 22:31:34

markers = p.circle_cross(...)返回需要指定为HoverTool的唯一呈现器的markers呈现器。查看两种可能的方法here

相关问题 更多 >