How to combine button with slider's current indice

Viewed 72

I would like to combine slider and button. Lets say we have 3 surfaces at 3 different depths. When i sliding through different depths i would like to toggle on/off additional traces at that particular depth with buttons. I created example below to explain it better.

import plotly.graph_objs as go

z1 = [
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4],
[4,4,4,4,4,4]
]

z2 = [[zij-1 for zij in zi] for zi in z1]
z3 = [[zij-2 for zij in zi] for zi in z1]



fig = go.Figure()

# Surface Traces
fig.add_trace(go.Surface(z=z1, colorscale='hsv', visible=False, showscale=False)),
fig.add_trace(go.Surface(z=z2, colorscale='hsv', visible=False, showscale=False)),
fig.add_trace(go.Surface(z=z3, colorscale='hsv', visible=False, showscale=False)),

# Scatter Traces
fig.add_trace(go.Scatter3d(x=[4],y=[6],z=[4.0],visible=False, showlegend=False, 
marker=dict(symbol='circle',size=10,color='red')))
fig.add_trace(go.Scatter3d(x=[4],y=[6],z=[3.0],visible=False, showlegend=False, 
marker=dict(symbol='circle',size=10,color='red')))
fig.add_trace(go.Scatter3d(x=[4],y=[6],z=[2.0],visible=False, showlegend=False, 
marker=dict(symbol='circle',size=10,color='red')))


# Visibility Options
#   Surface4km  Surface3km  Surface2km   Scatter4km  Scatter3km  Scatter2km
a = [True,      False,      False,       True,       False,      False]
b = [False,     True,       False,       False,      True,       False]
c = [False,     False,      True,        False,      False,      True]

# Show First Layer
fig.data[0].visible = True

# Create slider
depth=[4,3,2]

steps = []
for i in range(3):   

    step = dict(method="update",
                label = str(depth[i]),
                args = [{"visible": [False] * len(fig.data)},
                        {"title": "Depth: " + str(depth[i])}
                       ],  
               )

    step["args"][0]["visible"][i] = True  
    steps.append(step)


             
sliders = [dict(active=0, 
                currentvalue={"prefix": "Depth: ", "suffix": " km"},
                pad={"t": 50},
                steps=steps)
          ]



# Add dropdown
updatemenus=[dict(type = "buttons",
                  direction = "left",
                  buttons=list([dict(args=[{'visible':c}],
                                     method="restyle",
                                     label="Show Scatter"
                                     )
                                ]
                               ),
                  pad={"r": 0, "t": 30},
                  showactive=True,
                  x=0.11,
                  xanchor="left",
                  y=1.1,
                  yanchor="top"
                  )
             ]

fig.update_layout(scene=dict(zaxis=dict(range=[1, 5],autorange=False),
                             xaxis=dict(range=[0, 5],autorange=False),
                             yaxis=dict(range=[1, 14],autorange=False)
                             ),
              
                  sliders=sliders,
                  updatemenus=updatemenus
                  )


fig.show()

So initial figure starts at 4km layer. But args=[{‘visible’: c } which corresponds to 2km for surface and scatter trace. When i clicked the button it jumps 2km.But what i am trying to do is args=[{‘visible’: } part update itself depending on the slider poisition.

Initial state before click

After click

0 Answers
Related