如何在Bokeh HoverTool中仅显示值而不显示标签?
解决Bokeh HoverTool仅显示值的问题
要让HoverTool只显示对应的值(比如"A")而不是带标签的格式("Label:A"),只需修改HoverTool的tooltips参数,去掉键名部分,直接传入字段变量即可:
修改核心代码
原代码中HoverTool的定义:
hover = HoverTool(renderers=[circle_glyph], tooltips=[("Label", "@label")], formatters={"@label": "printf"})
将其修改为:
hover = HoverTool(renderers=[circle_glyph], tooltips=["@label"])
修改后的完整代码
from bokeh.plotting import figure, show, output_file from bokeh.models import HoverTool, ColumnDataSource # Sample data for circles x_values = [1, ] y_values = [6, ] circle_labels = ['A'] # Create a ColumnDataSource from the data source = ColumnDataSource(data=dict( x=x_values, y=y_values, label=circle_labels, )) # Create the figure p = figure(width=400, height=400, title="Circle Plot with Hover Tooltip") # Add circles to the plot circle_glyph = p.circle('x', 'y', size=20, color="navy", source=source) # Define hover tool with custom tooltip (仅显示值) hover = HoverTool(renderers=[circle_glyph], tooltips=["@label"]) # Add hover tool to the plot p.add_tools(hover) # Output and show the plot output_file("bokeh_plot.html", title='Bokeh Plot', mode='inline', root_dir=None) show(p)
原理说明:当tooltips传入仅包含字段变量(如@label)的列表时,Bokeh会直接显示该字段的对应值,不会添加前缀标签。
内容的提问来源于stack exchange,提问作者Eliya
相关产品推荐
相关产品推荐

