Plots with grouped legend

Hi @lspen, welcome to the forums. Something like this is possible, but has it’s limits.

the arguments you are looking for are legendgroup and legendgrouptitle

applied to your example:

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame(
    {
        "type1": ["a", "a", "a", "b", "b", "b"],
        "type2": ["c", "d", "c", "d", "c", "d"],
        "value": [1, 2, 3, 4, 5, 6],
    }
)
fig = go.Figure()
for i, (type1, type2) in enumerate(
    df[["type1", "type2"]].drop_duplicates().itertuples(index=False)
):
    fig.add_trace(
        go.Scatter(
            x=df.query("type1 == @type1 & type2 == @type2").index,
            y=df.query("type1 == @type1 & type2 == @type2").value,
            name=f"{type1}_{type2}",
            legendgroup=type1,
            legendgrouptitle={'text': type1}
        )
    )
fig.show()

creates:


mrep legend