Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 804
Balázs Berecz Avatar asked Nov 04 '25 16:11

Balázs Berecz


1 Answers

Setting the visible keyword argument of a trace to "legendonly" makes a line behave in the way you describe.

The 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

like image 145
Rob Raymond Avatar answered Nov 07 '25 11:11

Rob Raymond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!