Multiple traces with a single slider in plotly

Hi @sumins,

You will need to append all of your traces into a single (730 element) list. The trick is to make the arguments to your slider aware of the ordering of traces. You want the first step to enable traces 0 and 365, the second to enable 1 and 366, etc. Hereโ€™s an example with three slider steps and 6 total traces

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

num_steps = 3
trace_list1 = [
    go.Scatter(y=[1, 2, 3], visible=True, line={'color': 'red'}),
    go.Scatter(y=[3, 1, 1.5], visible=False, line={'color': 'red'}),
    go.Scatter(y=[2, 2, 1], visible=False, line={'color': 'red'})
]

trace_list2 = [
    go.Scatter(y=[1, 3, 2], visible=True, line={'color': 'blue'}),
    go.Scatter(y=[1.5, 2, 2.5], visible=False, line={'color': 'blue'}),
    go.Scatter(y=[2.5, 1.2, 2.9], visible=False, line={'color': 'blue'})
]

fig = go.Figure(data=trace_list1+trace_list2)

steps = []
for i in range(num_steps):
    # Hide all traces
    step = dict(
        method = 'restyle',  
        args = ['visible', [False] * len(fig.data)],
    )
    # Enable the two traces we want to see
    step['args'][1][i] = True
    step['args'][1][i+num_steps] = True
    
    # Add step to step list
    steps.append(step)

sliders = [dict(
    steps = steps,
)]

fig.layout.sliders = sliders

iplot(fig, show_link=False)

Hope that helps!
-Jon

1 Like