Set traces to be toggled off by default

Iā€™m creating a visualization that has 6 different histograms overlaid. By default, I would like only the first two to be visible. Then if the user chooses, they can click the traces in the legend to toggle the other 4 to be visible.

Is that possible?

1 Like

Hi @jmillz, this is possible : graph_objects traces have a visible attribute which can be set to legendonly, and then you can click on the trace name in the legend if you want them to become visible. For example

import plotly.graph_objects as go
fig = go.Figure(go.Scatter(y=[1, 2, 3]))
fig.add_trace(go.Scatter(y=[3, 4, 5], visible='legendonly'))
fig.show()

In fact we should add an example using legendonly to https://plot.ly/python/legend/ (unless you want to contribute it yourself of course!).

2 Likes