代码之家  ›  专栏  ›  技术社区  ›  PyRsquared

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

  •  1
  • PyRsquared  · 技术社区  · 7 年前

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

    enter image description here

    除了 Name 在那个文本框里? 为了简单起见,为了辩论,假设你 Mayor Largest Town 作为数据,想把它们也显示在下面 名字 . 以上面例子中的代码为例,假设我们有类似的东西(请参考所有代码的链接,我只是在这里使用一个示例)

    ...
    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 未知/未定义。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Pierre Chevallier    7 年前

    如何进行

    通过首先将其他变量的引用确实传递给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 .