Dropdown with N datasets and creating buttons

Hi @MalikaBanalika, welcome to the forums!

Here’s an example adapted from a similar slider example I made a while back (See Multiple traces with a single slider in plotly).

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)

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

fig.layout.updatemenus = [{'buttons': buttons}]

iplot(fig, show_link=False)

Hope that helps!
-Jon

2 Likes