Legend grouping not working with fig.add_shape

Hello everyone,

I’m not very experienced with Plotly, so pardon me if my question seems redundant or too obvious. I’m using Plotly to create some subplots, where I have several vertical lines and shapes that I want to toggle at the same time. The vertical lines were plotted using traces, so the legendgroup worked flawlessly, but I can’t seem to get it to work with the vrects and vlines.

# Add depots shadings
show_legend=True
for depot in tqdm(depots):
    mask = df_results['depot_drive'] == depot
    changes = mask.ne(mask.shift()).cumsum()
    for _, group in df_results[mask].groupby(changes[mask]):
        start = group['event_timestamp'].iloc[0]
        end = group['event_timestamp'].iloc[-1]
        color = depot_colors[depot]

        # Add shaded area to both plots
        for row in [1, 2, 3]:
            fig.add_vrect(
                x0=start,
                x1=end,
                fillcolor=color.replace('rgb', 'rgba').replace(')', ',0.2)'),
                layer="below",
                line_width=0,
                name="Depots",
                legend="legend1",
                showlegend=show_legend and row == 1,
                legendgroup="Depots",  # This groups all lines together
                legendgrouptitle_text= None,
                row=row, col=1
            )
            


            # Add vertical lines
            fig.add_vline(
                x=start,
                line_width=2,
                line_color=color,
                line_dash="solid",
                name="Depots",
                legend="legend1",
                showlegend=False,
                legendgroup="Depots",  # This groups all lines together
                legendgrouptitle_text= None,
                row=row, col=1
            )
            fig.add_vline(
                x=end,
                line_width=2,
                line_color=color,
                line_dash="solid",
                name="Depots",
                legend="legend1",
                showlegend=False,
                legendgroup="Depots",  # This groups all lines together
                legendgrouptitle_text= None,
                row=row, col=1
            )
        show_legend=False

This results with only the first rectangle being toggled on and off. Another thing I tried doing was creating an invisible trace with the same legendgroup and just setting all the shapes’ showlegends to False, but that didn’t seem to do the trick; it didn’t affect the plot.

Can anyone help me out with that? I’m starting to think maybe it’s a bug related to legends with shapes, because I did the exact same to the line traces and that worked without any issues.

Thanks!