Let’s suppose having a plot in a Plotly pane. I want to execute a handler when the user clicks the legend of the plot. According to the Documentation, watching the click_data
parameter should be the way to go, however nothing is happening for me there.
import panel as pn
import numpy as np
import plotly.graph_objs as go
import param
pn.extension("plotly")
class Plot(param.Parameterized):
def __init__(self):
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
scatter1 = go.Scatter(x=x, y=y1, mode='lines', name='Series 1 (sin)', line=dict(color='blue'))
scatter2 = go.Scatter(x=x, y=y2, mode='lines', name='Series 2 (cos)', line=dict(color='green'))
fig = go.Figure(data=[scatter1, scatter2], layout=go.Layout(title='Two Series Plot'))
self.plot = pn.pane.Plotly(fig)
self.plot.param.watch(self.__legend_click_handler__, "click_data")
def __legend_click_handler__(self, event):
print(event) # nothing is happening here, why?
@property
def ui(self):
return pn.Row(self.plot)
plot = Plot()
plot.ui