hvPlot - add arguments to hook function

I am trying to find a way to get a generalized hook function to customize hvPlots but with some flexibility. Basically, I would like to be able to add some parameters to the hook function

def my_hook(plot, element):
    f = plot.state
    color= '#A50021'
    f.xaxis.axis_line_color = color

data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)

df.hvplot.line().opts(hooks=[my_hook])

I would like, for example, to be able to pass ‘color’ as an argument for the ‘my_hook’ function instead of having to rewrite a new function for each color. Is there any way to do that ?

You could use functools.partial or a lambda function for this.

Thx a lot for your answer @Hoxbro ! Any chance you can point my to an example please ?

Something like this:

from functools import partial
def my_hook(plot, element, color='#A50021'):
    f = plot.state
    f.xaxis.axis_line_color = color

data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)

df.hvplot.line().opts(hooks=[partial(my_hook, color="black"])
2 Likes