Search code examples
pythonplotlydata-visualizationgraph-visualizationplotly-express

Plotly Express - plot subset of dataframe columns by default and the rest as option


I am using plotly express to plot figures this way :

    fig = px.line(df,
        x=df.index,
        y=df.columns
    )

It displays the graph properly and shows all the columns by default (as lines in the graph) with option to uncheck (or check) them to disable showing whatever we want if needed. What I would like is to show the same graph but by default uncheking some of the columns initially and keep the option to check or uncheck them for visualization. This means that I cannot take only a subset of columns as new data frame to show as the other columns are still relevant. Did not find anything in the documentation unfortunately...

Thank you in advance.


Solution

  • You can use the visible property of the traces to state it is only in the legend. Below shows all columns in the figure then first two columns are set as visible, all other columns are only in the legend.

    import plotly.express as px
    import pandas as pd
    import numpy as np
    
    # simulate dataframe
    df = pd.DataFrame(
        {c: np.random.uniform(0, 1, 100) + cn for cn, c in enumerate("ABCDEF")}
    )
    
    fig = px.line(df, x=df.index, y=df.columns)
    
    # for example only display first two columns of data frame, all others can be displayed
    # by clicking on legend item
    fig.for_each_trace(
        lambda t: t.update(visible=True if t.name in df.columns[:2] else "legendonly")
    )
    

    enter image description here