如果我们选择this geographic example from Bokeh documentation,我们会看到当您将鼠标悬停在县上时,德州郡名称会显示在文本框中,以及失业率和经度和纬度。
除了Name
之外,我们如何将多个其他标识符添加到该文本框中?为了简单起见,为了论证,我们假设您有Mayor
和{{ 1}}作为数据,并希望在Largest Town
下显示它们。以上示例中的代码为例,说我们有类似的东西(请参阅所有代码的链接,我只是在这里使用示例)
Name
虽然这不起作用,因为...
county_xs = [county["lons"] for county in counties.values()]
county_ys = [county["lats"] for county in counties.values()]
county_mayor = [county['mayor'] for county in counties.values()]
source = ColumnDataSource(data=dict(
x=county_xs,
y=county_ys,
name=county_names,
identifier_2 = county_mayor # guessing here
rate=county_rates,
))
...
hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
("Name", "@name"),
("Unemployment rate)", "@rate%"),
("Mayor", "@identifier_2"), # guessing here
("(Long, Lat)", "($x, $y)"),
]
未知/未定义。
答案 0 :(得分:2)
如何继续
您可以先将其他变量的引用添加到悬停工具,方法是先将它们传递给ColumnDataSource。
代码
from bokeh.io import show
from bokeh.models import (
ColumnDataSource,
HoverTool,
LogColorMapper
)
from bokeh.palettes import Viridis6 as palette
from bokeh.plotting import figure
from bokeh.sampledata.us_counties import data as counties
from bokeh.sampledata.unemployment import data as unemployment
palette.reverse()
counties = {
code: county for code, county in counties.items() if county["state"] == "tx"
}
county_xs = [county["lons"] for county in counties.values()]
county_ys = [county["lats"] for county in counties.values()]
# Creating a fake mayor variable
county_mayor = ["Mayor of " + county["name"] for county in counties.values()]
county_names = [county['name'] for county in counties.values()]
county_rates = [unemployment[county_id] for county_id in counties]
color_mapper = LogColorMapper(palette=palette)
# We add the mayor variable
source = ColumnDataSource(data=dict(
x=county_xs,
y=county_ys,
name=county_names,
rate=county_rates,
mayor=county_mayor,
))
TOOLS = "pan,wheel_zoom,reset,hover,save"
p = figure(
title="Texas Unemployment, 2009", tools=TOOLS,
x_axis_location=None, y_axis_location=None
)
p.grid.grid_line_color = None
p.patches('x', 'y', source=source,
fill_color={'field': 'rate', 'transform': color_mapper},
fill_alpha=0.7, line_color="white", line_width=0.5)
hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
# And we reference the mayor in the tooltip
hover.tooltips = [
("Name", "@name"),
("Unemployment rate)", "@rate%"),
("(Long, Lat)", "($x, $y)"),
("Mayor", "@mayor")
]
show(p)
<强>输出强>
<强>参考强>
此link from the documentation处的悬停工具参考。