[Python] How to trigger Hover(and tooltips) in multiple bokeh figures simultaneously?

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have multiple figures, sharing y-axis but not the x-axis. When my cursor hover on one figure, it will tell me the values (x & y-axis values). But Others have no tooltips. I want all figures show tooltips simultaneously when cursor hover any figure where the CrosshairTool intersect lines.

    Below, I posted the sample code that gives me result shown on the left.

    The image showing what I am getting with the code, and what I want

    Any help is highly appreciated!

    import numpy as np

    import pandas as pd

    from bokeh.plotting import figure, show
    from bokeh.layouts import row
    from bokeh.models import HoverTool, CrosshairTool, ColumnDataSource, Span

    df = pd.DataFrame({
    'depth': np.linspace(1000,1100,201),
    'x1': np.linspace(80,90,201)+np.random.rand(201),
    'x2': np.linspace(2.5,3.5,201)+np.random.rand(201)/10
    })

    source = ColumnDataSource(df)

    p1 = figure(y_range=(df['depth'].max(),df['depth'].min()),
    width=400,height=600,y_axis_label='Depth (m)')

    p2 = figure(y_range=p1.y_range,width=400, height=600)

    p1.line('x1','depth',source=source, line_width=2, color='blue')
    p2.line('x2','depth',source=source, line_width=2, color='green')

    hover1 = HoverTool(
    tooltips=[('', '@depth{0.0} m'),('', '@x1{0.0} °C')],
    mode='hline',attachment="above"
    )

    hover2 = HoverTool(
    tooltips=[('','@depth{0.00} m'),('','@x2{0.00} µs/ft')],
    mode='hline',attachment="above"
    )

    xspan = Span(dimension="width",line_width=0.5)

    p1.add_tools(CrosshairTool(overlay=xspan))
    p2.add_tools(CrosshairTool(overlay=xspan))

    p1.add_tools(hover1)
    p2.add_tools(hover2)

    layout = row(p1,p2)

    show(layout)

    Continue reading...

Compartilhe esta Página