散景工具提示不显示玩家姓名,但正确显示其他数据

问题描述 投票:0回答:1

我正在使用 Jupyter Notebook 中的 Bokeh 创建散点图来可视化玩家数据。当我将鼠标悬停在数据点上时,散点图应在工具提示中显示玩家名称。但是,我遇到了一个问题,即玩家姓名未显示在工具提示中,但其他数据(例如工资和价值)显示正确。

这是我正在使用的代码:

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

# Data
from bokeh.plotting import figure, show
from bokeh.models import HoverTool
x = player_info['Wage']
y = player_info['Value']
Name = player_info['Name'].astype(str)
# Hover tooltips
TOOLTIPS = HoverTool(tooltips=[
    ("index", "$index"),
    ("(Wage,Value)", "(@x, @y)"),
    ("Name", "@Name")
])
# Creating the plot
p = figure(title='soccer', width=400, height=400,x_axis_label='Wage', y_axis_label='Value',         tools=[TOOLTIPS])
p.circle(x, y, fill_color='red', size=10)
# Show the plot
show(p)

output screenshot

我已尝试多种故障排除步骤来解决该问题,包括:

1.数据验证:确保player_info['Name']包含有效的玩家名称字符串。

2.显式字符串转换: 使用 .astype(str) 将 Name 列显式转换为字符串,以确保它包含字符串数据。

3.Bokeh 更新: 使用 pip install --upgrade bokeh 将 Bokeh 更新到最新版本。

4.内核重启: 重新启动 Jupyter Notebook 内核以排除与笔记本状态相关的任何问题。

5.浏览器检查:在运行代码时检查浏览器控制台是否存在 JavaScript 错误。没有报告 JavaScript 错误。

6.浏览器兼容性:在不同的网络浏览器中测试代码,以确保问题不是特定于浏览器的。

尽管做出了这些努力,玩家姓名仍然无法在工具提示中正确显示,并且我仍然看到“姓名:??”当鼠标悬停在数据点上时

python bokeh
1个回答
0
投票

这里有一个 Python 代码片段,使用 Bokeh 创建带有工具提示的足球运动员数据散点图。此代码假设您有一个名为player_info 的 DataFrame,其中包含“Wage”、“Value”和“Name”列。该图将显示球员工资与其市场价值之间的关系,当鼠标悬停在数据点上时,工具提示会显示球员姓名、指数、工资和价值。

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

# Assuming you have a DataFrame named player_info
x = player_info['Wage']
y = player_info['Value']
Name = player_info['Name']

# Create a ColumnDataSource to hold the data
source = ColumnDataSource(data=dict(x=x, y=y, Name=Name))

# Define tooltips for the HoverTool
TOOLTIPS = HoverTool(tooltips=[
    ("index", "$index"),
    ("Name", "@Name"),
    ("(Wage, Value)", "(@x, @y)"),
])

# Create the figure
p = figure(title='Soccer Player Data', width=400, height=400, 
x_axis_label='Wage', y_axis_label='Value', tools=[TOOLTIPS])

# Add circles to the plot
p.circle(source=source, fill_color='red', size=10)

# Show the plot
show(p)

说明:

1.我们从 Bokeh 导入必要的模块:figure、show、HoverTool 和 ColumnDataSource。

2.我们从player_info DataFrame中提取“Wage”、“Value”和“Name”列,并创建一个ColumnDataSource来保存这些数据。

3.我们为HoverTool定义工具提示,以在将鼠标悬停在数据点上时显示玩家的索引、姓名以及工资和价值。

4.我们创建一个带有 x 轴和 y 轴标签的图形对象,并将 HoverTool 添加到工具列表中。

5.最后,我们使用circle将数据点添加到绘图中,并使用show来显示绘图。

© www.soinside.com 2019 - 2024. All rights reserved.