python - 如何使用 Bokeh 在绘图上显示各种悬停标识符?

标签 python bokeh

如果我们取 this geographic example from Bokeh documentation ,当您将鼠标悬停在德克萨斯州县上时,我们会看到文本框中显示该县的名称以及失业率以及经度和纬度。

enter image description here

我们如何将除Name之外的多个其他标识符放入该文本框中?为了简单起见,为了便于讨论,假设您有MayorLargest 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)"),
]

尽管这不起作用,因为 identifier_2 未知/未定义。

最佳答案

如何进行

您可以通过首先将对其他变量的引用添加到悬停工具,方法是将它们传递给 ColumnDataSource。

  1. 首先将数据添加到ColumnDataSource(确保变量与其他变量具有相同的维度)
  2. 将来源添加到您的图中
  3. 随后在悬停工具内引用它

代码

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)    

输出

预期结果应该是这样的: enter image description here

引用

悬停工具引用link from the documentation .

关于python - 如何使用 Bokeh 在绘图上显示各种悬停标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50817533/

相关文章:

python - pandas 从头部和尾部获取 k 个条目

python - 如何在 bokeh python 中捕获下拉小部件的值?

python - 重复调用 Python Scikit 分类器 `.fit` 方法是否会产生任何影响?

python - 带有 Bokeh 图的 ImportError : No module named _ctypes. Google 应用引擎

Python Bokeh : How to update a Toggle button - defined in the main - in a subroutine

python - 在 Plotly 或 Python 中的 Bokeh 中的 map 上绘制散点饼图

python - 带 Bokeh 的条形图 pandas Dataframe

python - 是 Django 部署中的目录错误

python - 我需要减慢 python tkinter 应用程序中的循环

python - 如何通过QFileDialog获取pyqt6中的目录路径?