I am trying to replace the Plotly provided legend with a custom legend using ipywidgets. A simple example is below where I have a checkbox for each trace that can be used to "highlight" the trace from the legend.
This seems to work fine, but I would like to reproduce something similar to what Plotly would normally show for the given trace. I can get all the info I need, line color, dash type, marker symbol, etc, but I can't see a easy way to reproduce a legend entry graphic such that it looks like what Plotly would have produced.
Here is what I have so far, which should be a self contained example:
import ipywidgets as widgets
from ipywidgets import interactive, fixed
import plotly.graph_objects as go
line = {'name': 'line','data': ((1,1), (2,2), (3,3)), 'color':'red', 'dash':'solid', 'symbol':'circle'}
square = {'name': 'square','data': ((1,1), (2,4), (3,9)), 'color':'blue', 'dash':'dash', 'symbol':'square'}
traces = (line, square)
def get_values(func, index):
return [e[index] for e in func['data']]
def toggle_highlight(highlight, fig, index):
new_width = 4 if fig.data[index].line.width == 2 else 2
fig.data[index].line.width = new_width
fig = go.FigureWidget()
legend_items = []
for i, t in enumerate(traces):
highlight_chk = interactive(toggle_highlight, highlight=False, fig=fixed(fig), index=fixed(i)).children[0]
item = widgets.HBox([widgets.Label(value=f"{t['name']}, c = {t['color']}, d = {t['dash']}, s = {t['symbol']}"), highlight_chk])
s = go.Scatter(name=t['name'], x=get_values(t, 0), y=get_values(t, 1), line=dict(width=2, color=t['color'], dash=t['dash']), marker=dict(symbol=t['symbol']), customdata=[{'index':i}])
fig.add_trace(s)
legend_items.append(item)
legend = widgets.VBox(legend_items)
display(legend)
display(fig)
This is what it looks like now:

this is what I would like to get it to look like:

Any thoughts on how to create such a "label"?
I was able to accomplish my goal using an HTML widget containing an SVG line. Below is an example where I replaced the Plotly legend with widgets that allow me to make any trace "normal", "highlighted" or invisible.
Here is a screenshot of what it looks like.