Search code examples
pythonplotlyhideshow

How to disable a line in plotly at start?


I would like to show the lines but with some are disabled. So just like when I show it normally and then click on its name to unshow/disable the line. I am using python.


Solution

  • visible attribute of a trace as "legendonly" makes a line behave in way you describe

    Below code generates a figure with 10 lines, then sets visible to legendonly for lines 3 to 10. Clicking on legend makes them visible.

    import pandas as pd
    import numpy as np
    import plotly.express as px
    
    df = pd.DataFrame({f"line{i+1}":np.random.uniform(i,i+2,100) for i in range(10)})
    px.line(df, x=df.index, y=df.columns).update_traces(visible="legendonly", selector=lambda t: not t.name in ["line1","line2"])
    

    enter image description here