Keep data while using dropdown menu

HI there,

I would like to use the dropdown button as a data filter to select some data series. However, once I change the menu, the plot disappears.

How can I keep the previous data if it was already visible?

Here is MRE:

import plotly.graph_objects as go
import numpy as np

fig = go.Figure()


for i in range(1,4):
    fig.add_trace(
                            go.Scatter3d
                            (
                                x=np.linspace(0, 1),
                                y=np.linspace(0, 1),
                                z=i*np.linspace(0, 1),
                                mode='markers',
                                name=f"g1_{i}",
                                visible = "legendonly",
                                legendgroup="" ,
                            )
                        )
    
for i in range(4,7):
    fig.add_trace(
                            go.Scatter3d
                            (
                                x=np.linspace(0, 1),
                                y=np.linspace(0, 1),
                                z=i*np.linspace(0, 1),
                                mode='markers',
                                name=f"g2_{i}",
                                visible = "legendonly",
                                legendgroup="" ,
                            )
                        )
    
for i in range(7,10):
    fig.add_trace(
                            go.Scatter3d
                            (
                                x=np.linspace(0, 1),
                                y=np.linspace(0, 1),
                                z=i*np.linspace(0, 1),
                                mode='markers',
                                name=f"g3_{i}",
                                visible = "legendonly",
                                legendgroup="" ,
                            )
                        )
    
fig.update_layout(  
        updatemenus=[
                        dict(    
                            buttons=list([
                                dict(
                                    args=[{"visible": [True if trace.visible else "legendonly" for trace in fig['data']]}],
                                    label=("All"),
                                    method="restyle",
                                ),
                                dict(
                                    args=[{"visible": ["legendonly" if ("g1" in trace.name) else False for trace in fig['data']]}],
                                    label="g1",
                                    method="restyle",
                                ),
                                dict(
                                    args=[{"visible": ["legendonly" if ("g2" in trace.name) else False for trace in fig['data']]}],
                                    label="g2",
                                    method="restyle",
                                ), 
                                dict(
                                    args=[{"visible": ["legendonly" if ("g3" in trace.name) else False for trace in fig['data']]}],
                                    label="g3",
                                    method="restyle",
                                ),                                 
                            ]),
                            direction = "down",
                            pad={"r": 10, "t": 10},
                            showactive=True,
                            x=0.75,
                            xanchor="center",
                            y=1.1,
                            yanchor="top"
                        )])

Also the β€œAll” button just selects everything and does check work as intended. If the trace is visible keep it visible otherwise put it as β€œlegendonly”.

Would appreciate some insight into this.