2

Is it possible to "link" the hover of two bokeh / holoview objects so the hover over the two objects are displayed at the same time?

Here is an example of what I would like to achieve:

enter image description here

Currently I can only display the hover on one of the image but not on both at the same time: enter image description here

Please find below the reproducible code I used to generated this last figure.

import xarray as xr
import hvplot.xarray  # noqa

import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

import panel as pn
import panel.widgets as pnw
pn.extension()

from bokeh.models import HoverTool

# Load tutorial xarray air dataset
air_ds = xr.tutorial.open_dataset('air_temperature').load()

# Create two different datasets based on the yearly mean and standard deviation of the  air temperature
meanairbyyear = air_ds.air.groupby('time.year').mean()
stdairbyyear = air_ds.air.groupby('time.year').std()
meanair2d = xr.Dataset(
    {
        "y2013": (["lat", "lon"], meanairbyyear[0,:,:]),
        "y2014": (["lat", "lon"], meanairbyyear[1,:,:]),
    },
    coords={
        "lon": ("lon", meanairbyyear.lon),
        "lat": ("lat", meanairbyyear.lat),
    },
)
stdair2d = xr.Dataset(
    {
        "y2013": (["lat", "lon"], stdairbyyear[0,:,:]),
        "y2014": (["lat", "lon"], stdairbyyear[1,:,:]),
    },
    coords={
        "lon": ("lon", stdairbyyear.lon),
        "lat": ("lat", stdairbyyear.lat),
    },
)

# Define panel object
variable_type = ('y2013', 'y2014')

variables  = pnw.RadioButtonGroup(name='Variable', value='y2014', 
                                 options=list(variable_type))

def plot_map_mean(var):
    plt = meanair2d[var].hvplot.contourf(width=400)
    # Hover
    MyHover = HoverTool(
        tooltips=[
            ( 'Lon', ' $x'),
            ( 'Lat', ' $y'),
            ( var + ' Mean', '@' + var),
       ],
        formatters={
            '$x' : 'numeral',
            '$y' : 'numeral',
            '@' + var : 'numeral',            
        },
        point_policy="follow_mouse"
    )
    plt.opts(tools = [MyHover])
    return plt

def plot_map_std(var):
    plt = stdair2d[var].hvplot.contourf(width=400)
    # Hover
    MyHover = HoverTool(
        tooltips=[
            ( 'Lon', ' $x'),
            ( 'Lat', ' $y'),
            ( var + ' Std', '@' + var),
       ],
        formatters={
            '$x' : 'numeral',
            '$y' : 'numeral',
            '@' + var : 'numeral',            
        },
        point_policy="follow_mouse"
    )
    plt.opts(tools = [MyHover])
    return plt

@pn.depends(variables)
def reactive_mean_plot(variables):
    return plot_map_mean(variables)

@pn.depends(variables)
def reactive_std_plot(variables):
    return plot_map_std(variables)

widgets   = pn.Column("<br>\n# Years", variables)
contour_pn = pn.Column(widgets,pn.Column("<br>\n### Mean",reactive_mean_plot),
                               pn.Column("<br>\n### Std",reactive_std_plot)
                      )
contour_pn
lhoupert
  • 584
  • 8
  • 25
  • You could certainly fake it by using a PointerXY stream to display some text on both plots, with one plot as the master for pointer location, but I don't know of any way that the standard hover support could be used in this way. – James A. Bednar Jan 09 '21 at 18:29

0 Answers0