How do you hide some traces initially in Plotly Express?

I have graphs with lots of traces. It would be nice to hide the not-essential traces as default.

By hiding I mean that traces are not visible, and the legend entry is dimmed. I.e. the same state that happens when user clicks an legend entry.

Note that there is an earlier question on almost the same topic, but not quite (How do you hide traces initially in Plotly Express?). I want to initially hide only few traces and leave the others visible.

1 Like

Hi @perza and welcome to the forum! :tada:

You can apply the same logic but only to some of the traces, like this:

fig.update_traces(visible='legendonly', selector=dict(name="d"))

Full example:

from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2)

fig.add_scatter(y=[4, 2, 3.5], mode="markers",
                marker=dict(size=20, color="LightSeaGreen"),
                name="a", row=1, col=1)

fig.add_bar(y=[2, 1, 3],
            marker=dict(color="MediumPurple"),
            name="b", row=1, col=1)

fig.add_scatter(y=[2, 3.5, 4], mode="markers",
                marker=dict(size=20, color="MediumPurple"),
                name="c", row=1, col=2)

fig.add_bar(y=[1, 3, 2],
            marker=dict(color="LightSeaGreen"),
            name="d", row=1, col=2)

fig.update_traces(visible='legendonly',
                  selector=dict(name="d"))

fig.show()
2 Likes

Great, thanks a lot!