Search code examples
pythonplotlyplotly-dash

How to add button to plotly chart to toggle multi traces?


how to add button like this below to toggle data from all traces on the scatter? toggle buttons

Thanks, Paulina


Solution

  • The hover text data comparison button on the menu bar does not appear to be visible at all times. I do not know why. Adding the following code will make it appear. This tips was confirmed from here.

    import plotly.graph_objects as go
    
    # Create random data with numpy
    import numpy as np
    np.random.seed(1)
    
    N = 100
    random_x = np.linspace(0, 1, N)
    random_y0 = np.random.randn(N) + 5
    random_y1 = np.random.randn(N)
    random_y2 = np.random.randn(N) - 5
    
    fig = go.Figure()
    
    # Add traces
    fig.add_trace(go.Scatter(x=random_x, y=random_y0,
                        mode='markers',
                        name='markers'))
    fig.add_trace(go.Scatter(x=random_x, y=random_y1,
                        mode='lines+markers',
                        name='lines+markers'))
    fig.add_trace(go.Scatter(x=random_x, y=random_y2,
                        mode='lines',
                        name='lines'))
    
    
    fig.update_layout(modebar_add=[
        "v1hovermode",
        "toggleSpikelines",
    ])
    
    fig
    

    enter image description here