在 Python 中使用 Hovertool 和 Bokeh

Using Hovertool in Python with Bokeh

我是一名新 python 学习者,我正在尝试用散景制作情节。我想使用悬停工具,当我在点上滚动时它正在工作。但是,X 和 Y 值显示 ???而不是实际值。我不太确定我做错了什么,因为 hovertool 本身正在工作,但没有显示值。

    from bokeh.plotting import figure`
    from bokeh.io import show, output_notebook

    get_provider(Vendors.CARTODBPOSITRON)
    from bokeh.models import ColumnDataSource, HoverTool  

    # Create a blank figure with labels
    p = figure(plot_width = 600, plot_height = 600, 
               title = 'Example Glyphs',
               x_axis_label = 'X', y_axis_label = 'Y')


    hover = HoverTool(tooltips=[
        ("X", "@X "),
        ("Y","@Y")])

    p = figure(x_axis_type="mercator", 
               y_axis_type="mercator",
               tools=[hover, 'wheel_zoom','save']) 

    p.add_tile(CARTODBPOSITRON)

    # Example data
    circles_x = [1, 3, 4, 5, 8]
    circles_y = [8, 7, 3, 1, 10]
    circles_x = [9, 12, 4, 3, 15]
    circles_y = [8, 4, 11, 6, 10]

    # Add squares glyph
    p.circle(squares_x, squares_y, size = 12, color = 'navy', alpha = 0.6)
    # Add circle glyph
    p.circle(circles_x, circles_y, size = 12, color = 'red')

    # Set to output the plot in the notebook
    output_notebook()
    # Show the plot
    show(p)

如果您没有使用明确的 ColumnDataSource(这将允许您使用和引用您想要的任何列名),那么您必须引用 Bokeh 使用的默认列名。在这种情况下,对于 circle,默认的列名称是 "x""y"(小写,而不是上面的大写)。所以:

hover = HoverTool(tooltips=[
    ("X", "@x"),
    ("Y", "@y"),
])