'Plotly with python - line chart deselect all

I got a line chart, with multiple lines that represent sine waves with different frequencies.

I want to take a look at a specific wave, with all the rest out of the graph. I know that I can click, in the legend, on the lines I don't want to see and it will make them go away.

I was wondering if there is an interactive way to deselect all lines in one click, instead of clicking each one of them.

My code:

import numpy as np
import plotly.graph_objects as go

step = 1/1000
t = np.arange(0,1,step)    # time vector
trig_func = lambda ff : np.sin(2*np.pi*ff*t)

fig = go.Figure()

for freq in [1, 3, 5, 7]:
    y = trig_func(freq)
    fig.add_trace(go.Scatter(x=t, y=y, name=f"{freq} Hz"))

fig.show()

My graph: enter image description here

Desired graph: enter image description here



Solution 1:[1]

Before you show the figure, add this part:

fig.update_layout(dict(updatemenus=[
                        dict(
                            type = "buttons",
                            direction = "left",
                            buttons=list([
                                dict(
                                    args=["visible", "legendonly"],
                                    label="Deselect All",
                                    method="restyle"
                                ),
                                dict(
                                    args=["visible", True],
                                    label="Select All",
                                    method="restyle"
                                )
                            ]),
                            pad={"r": 10, "t": 10},
                            showactive=False,
                            x=1,
                            xanchor="right",
                            y=1.1,
                            yanchor="top"
                        ),
                    ]
              ))

This will add buttons to deselect and select all traces at once. It should look like this:

Result Chart

More on custom buttons in plotly: https://plotly.com/python/custom-buttons/

Solution 2:[2]

It seems that double clicking a legend can deselect all legends now. I am testing the questioner's code in google colab. The plotly version is 5.5.0. The trick is that make sure the legend is selected before double click. And the double click need to be very fast, faster than normal.
After selecting single legend, you can also double click it to select all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney
Solution 2 Ben2018