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

Bokeh悬停提示框无法显示球员姓名,其他数据正常显示

问题原因与解决方案

核心问题是你定义的Name变量没有关联到散点图的图形元素上,Bokeh无法自动识别独立的变量,必须将所有需要在悬停提示中展示的数据绑定到同一个数据源中。

修复方案1:使用ColumnDataSource(推荐)

这是Bokeh处理数据的标准方式,能更清晰地管理所有关联数据:

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

# 准备数据
x = player_info['Wage']
y = player_info['Value']
names = player_info['Name'].astype(str)

# 创建统一数据源
source = ColumnDataSource(data={
    'Wage': x,
    'Value': y,
    'Name': names
})

# 配置悬停提示
hover_tool = HoverTool(tooltips=[
    ("index", "$index"),
    ("(Wage,Value)", "(@Wage, @Value)"),
    ("Name", "@Name")
])

# 创建图表并绑定数据源
p = figure(
    title='soccer', 
    width=400, 
    height=400,
    x_axis_label='Wage', 
    y_axis_label='Value',
    tools=[hover_tool]
)
p.circle('Wage', 'Value', fill_color='red', size=10, source=source)

show(p)

修复方案2:直接在circle中传递数据字段

如果不想单独创建ColumnDataSource,可以在调用circle时直接传入Name数据:

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

# 准备数据
x = player_info['Wage']
y = player_info['Value']
names = player_info['Name'].astype(str)

# 配置悬停提示
hover_tool = HoverTool(tooltips=[
    ("index", "$index"),
    ("(Wage,Value)", "(@x, @y)"),
    ("Name", "@Name")
])

# 创建图表,在circle中传入Name数据
p = figure(
    title='soccer', 
    width=400, 
    height=400,
    x_axis_label='Wage', 
    y_axis_label='Value',
    tools=[hover_tool]
)
# 通过额外参数传递Name,Bokeh会自动关联
p.circle(x, y, fill_color='red', size=10, Name=names)

show(p)

关键说明

  • 原代码中Name变量只是单独定义,没有被circle方法接收,导致HoverTool无法找到对应的字段,因此显示??。
  • 使用ColumnDataSource能让数据管理更规范,尤其在处理多字段数据时更易维护。

内容的提问来源于stack exchange,提问作者Muhammad Yousaf Rana

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:42:15