Panel slider and Holoviews linking

Hi everyone,

I would like to link a holoviews plot with a change in panel Slider widget. Following is a representative code example:

import numpy as np
import pandas as pd

import panel as pn
import holoviews as hv

pn.extension()
hv.extension('bokeh')

N = 100
slider_w = pn.widgets.IntSlider(name="Index", start=0, end=N)

T = np.linspace(10,100, N)
V = 5*sin(T/5) + 10*np.random.rand(len(T))

df = pd.DataFrame({"Temperature": T, "Voltage": V})fig_line = hv.Curve(data=df)

# Line plot overlaid with a point
fig_line = hv.Curve(data=df)
fig_points = hv.Points([df.iloc[slider_w.value].values])
fig_points.opts(color='k', marker='o', size=5)
fig = fig_line * fig_points
fig.opts(width=500)

# Callback
def callback(target, event):
    temp, voltage = df.iloc[event.new].values
    target['x'] = temp
    target['y'] = voltage
    
    
slider_w.link(fig_points.data, callbacks={'value':callback})

app = pn.Column(
    title_pane, 
    pn.Row(
        slider_w, 
        button_w
    ),
    fig
)

How can I link the slider with the point data such that the plot gets updated? I am sorry for asking probably a too simple question. Thanks for your patience.

Best regards,
Sam


N = 100
slider_w = pn.widgets.IntSlider(name="Index", start=0, end=N-1)

T = np.linspace(10,100, N)
V = 5*np.sin(T/5) + 10*np.random.rand(len(T))

df = pd.DataFrame({"Temperature": T, "Voltage": V})
fig_line = hv.Curve(data=df)

# Line plot overlaid with a point
fig_line = hv.Curve(data=df)

@pn.depends(idx=slider_w)
def points(idx):
    return hv.Points([df.iloc[idx].values])

fig_points = hv.DynamicMap(points)
fig_points.opts(color='k', marker='o', size=5)
fig = fig_line * fig_points

pn.Row(slider_w, fig.opts(width=500))
2 Likes

Thanks @xavArtley

I only wanted to know how we can use callbacks for the same. I noticed that in bigger applications with several plots and widgets getting linked, the pn.bind method becomes slower. Does pn.depends not be slower in the same way?

Would you have a suggestion how this can be done using callbacks?

Best regards,
Sam

1 Like

Hi @sam_panel

Could you be specific about the performance problems you are experiencing?

A code example and some way to reproduce the poor performance would enable us to help.

Thanks

Hi @Marc

I checked the documentation in greater detail after going through the code of @xavArtley and just discovered the missing link - hv.DynamicMap. I think my plots were slow just because I wasn’t using hv.DynamicMap when I was just using pn.bind blindly which was replotting every time. I found a solution using bokeh plots where I used a callback function to update the data of the circle component of the figure. But I wanted to find a solution entirely using holoviews wherein I can just update the data. Now I found the solution.

I was wondering nevertheless, what was missing in my code though? I did something similar in bokeh plot figure’s circle component where it worked.

Here is snippet of what I had coded (full code is up in the thread):

...
# Callback
def callback(target, event):
    temp, voltage = df.iloc[event.new].values
    target['x'] = temp
    target['y'] = voltage
    
    
slider_w.link(fig_points.data, callbacks={'value':callback})
...

Many thanks.

Sam