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

如何消除Python Holoviews弦图(Chord Diagram)标签悬停弹出的弹窗

解决Holoviews弦图标签悬停弹窗不消失问题的方案

这个问题是Holoviews使用Bokeh后端渲染弦图时的已知bug,标签元素没有正确绑定鼠标移出事件,导致触发的悬停弹窗不会自动消失,以下是三个可直接复用的绕过方案:

  • 方案1:禁用标签悬停提示(最简单,推荐优先使用)
    仅需要在Chord的opts配置中新增label_hover=False参数,即可从根源上避免弹窗弹出,不影响标签正常显示和其他功能。修改后的配置代码如下:
chord.opts(
    opts.Chord(cmap='Category10', 
               edge_color=dim('Target').str(), 
               node_color=dim('name').str(),
               labels='name',
               label_text_color="white",
               label_hover=False, # 新增此行禁用标签悬停
               hooks=[rotate_label]
               ))
  • 方案2:保留悬停功能,通过钩子注入JS修复事件
    如果需要保留标签悬停显示信息的功能,可以在现有的rotate_label钩子函数中注入JS逻辑,绑定鼠标移出事件主动清空弹窗,修改后的代码如下:
    首先需要先导入Bokeh的CustomJS模块:
from bokeh.models import CustomJS

再修改钩子函数:

def rotate_label(plot, element):
    text_cds = plot.handles['text_1_source']
    length = len(text_cds.data['angle'])
    text_cds.data['angle'] = [0]*length
    xs = text_cds.data['x']
    text = np.array(text_cds.data['text'])
    xs[xs<0] -= np.array([len(t)*0.019 for t in text[xs<0]])
    
    # 新增JS逻辑修复弹窗不消失问题
    text_renderer = plot.handles['text_1']
    hover_tool = plot.state.toolbar.active_inspectors[0]
    plot.state.js_on_event('mousemove', CustomJS(args=dict(hover=hover_tool, text_render=text_renderer),
        code="""
            if (cb_data.event.target != text_render) {
                hover.clear();
            }
        """))
  • 方案3:全局关闭悬停工具
    如果不需要任何悬停提示,可直接在opts配置中指定需要的工具,排除悬停工具即可:
chord.opts(
    opts.Chord(cmap='Category10', 
               edge_color=dim('Target').str(), 
               node_color=dim('name').str(),
               labels='name',
               label_text_color="white",
               tools=['pan','wheel_zoom','reset'], # 仅保留指定工具,移除默认的悬停工具
               hooks=[rotate_label]
               ))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 10:54:00