You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bokeh3中HoverTool提示框透明背景及样式自定义问题

解决Bokeh 3中HoverTool提示框透明背景及自定义样式问题

Bokeh 3重构了工具提示的DOM结构,原Bokeh 2中的bk-tooltip类被拆分为bk-tooltip-wrapper(外层容器)和bk-tooltip-content(内容容器),默认样式优先级更高,直接修改bk-tooltip-content无法完全覆盖背景样式。以下是两种可行的解决方案:

方案一:在自定义Tooltip HTML中通过:host选择器覆盖外层容器样式

利用:host选择器定位HoverTool提示框的外层宿主容器bk-tooltip-wrapper,同时修改内容容器样式,确保背景透明及自定义样式生效:

from bokeh.io import show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure

source = ColumnDataSource(data=dict(x=[0,1,2,3,4],y=[1,1,1,1,1]))

p = figure(tools='pan,wheel_zoom,reset')
line = p.line(x='x',y='y', source=source)

hover = HoverTool(tooltips="""
<div style="display: table;">
  <div style="display: table-row;">
    <div style="display: table-cell; text-align:right;">
      x:        
    </div>
    <div style="display: table-cell;text-align:center;">
      @y
    </div>
  </div>
</div>
<style>
  :host(.bk-tooltip-wrapper) {
    background-color: transparent !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
  }
  .bk-tooltip-content {
    background-color: transparent !important;
    color: white !important;
    text-align: center !important;
    font-family: arial !important;
    font-size: 1.15em !important;
    font-weight: 600 !important;
    padding: 0 !important;
    margin: 0 !important;
    border: none !important;
  }
</style>
""", renderers=[line])

p.add_tools(hover)
show(p)

关键说明:

  • :host(.bk-tooltip-wrapper) 直接选中提示框的外层容器,覆盖其默认背景色、边框等样式
  • 同时修改bk-tooltip-content的样式,确保内容区域的字体、颜色等符合需求
  • 修复了你原代码中border : 0px important;的语法错误,需写为!important

方案二:全局注入自定义CSS(适合独立HTML或Bokeh Server场景)

如果需要全局生效所有HoverTool的样式,可以通过output_file的css_raw参数注入自定义CSS,或在Bokeh Server的模板中添加样式:

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure

# 注入全局自定义CSS
output_file("custom_hover.html", css_raw=[
    """
    .bk-tooltip-wrapper {
        background-color: transparent !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }
    .bk-tooltip-content {
        background-color: transparent !important;
        color: white !important;
        text-align: center !important;
        font-family: arial !important;
        font-size: 1.15em !important;
        font-weight: 600 !important;
        padding: 0 !important;
        margin: 0 !important;
        border: none !important;
    }
    """
])

source = ColumnDataSource(data=dict(x=[0,1,2,3,4],y=[1,1,1,1,1]))
p = figure(tools='pan,wheel_zoom,reset')
line = p.line(x='x',y='y', source=source)

hover = HoverTool(tooltips="""
<div style="display: table;">
  <div style="display: table-row;">
    <div style="display: table-cell; text-align:right;">
      x:        
    </div>
    <div style="display: table-cell;text-align:center;">
      @y
    </div>
  </div>
</div>
""", renderers=[line])

p.add_tools(hover)
show(p)

关键说明:

  • 此方法会对所有HoverTool提示框生效,适合统一样式需求
  • 若使用Bokeh Server,可将CSS添加到自定义HTML模板中,效果一致

内容的提问来源于stack exchange,提问作者vpvp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 09:35:32