【问题标题】:Using Hovertool in Python with Bokeh在 Python 中使用 Hovertool 和 Bokeh
【发布时间】:2020-03-08 19:30:51
【问题描述】:

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

    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)

【问题讨论】:

    标签: python bokeh


    【解决方案1】:

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

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

    【讨论】:

    • 您应该更新您的问题,使其与您遇到的实际问题有关。
    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多