How do you hide traces initially in Plotly Express?

I have a visualization that has many categories (100+) contained within a single column. I want to display them on plotly express so I pass that column as color= column_w_100+_category.

However, I don’t want to show all of them together at once at the start. Ideally, it should be an empty chart in the beginning that you can slowly click on the legend to populate it.

Plotly sort of teaches you the way to do it with plotly.go, but it is impractical to add 100 trace one by one so I did it with plotly express. I searched online and it seems like there is no discussion about how to do this at plotly express.

Alternatively, I can just use for loop add trace for plotly.go, but it will take a long time to redevelop the charting code…

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    visible='legendonly'
))

fig.show()

So all of the go attributes can be applied to any figure (created with go or px) with the update_traces methods on fig. So you can do something like:

px.scatter(…).update_traces(visible=‘legendonly’)
1 Like