Search code examples
pandasdrop-down-menuplotly

Plotly Dropdown not updating graphs correctly


I am working on creating dropdown menu for my data here is my data

data = {'Time': [2,4,5,6,7], 'Voltage': [20.3, 17.2,15.3,9.4,2], "Current":[2, 5,7,8,9]}  
df = pd.DataFrame(data)

this is the code for plotting dropdowns

plot = px.Figure(data=[go.Scatter( 

    name='Voltage', 

    x=df["Time"], 

    y=df["Voltage"]
), 

    go.Scatter( 

    name='Data 2', 

    x=df["Time"], 

    y=df["Current"] 
) 
]) 

plot.update_layout( 

    updatemenus=[ 

        dict( 
            buttons=list([ 

                dict(label="Voltage", 

                     method="update", 

                     args=[{"visible": [True, False]}, 

                           {"title": "Voltage", 

                            }]), 

                dict(label="Current", 

                     method="update", 

                     args=[{"visible": [False, True]}, 

                           {"title": "Data 2", 

                            }]), 

            ]), 

        ) 

    ]) 
plot.show() 

even though i am not selecting both plots to display it is showing both plots how to overcome this thanks and regards


Solution

  • You need to add visible = Falseto second scatter. So it should be :

    go.Scatter( 
    
    name='Data 2', 
    
    x=df["Time"], 
    
    y=df["Current"] ,
    
    visible = False)