I'm new to plotly and I am trying to understand how to set the values for args and args2 in the updatemenus functionality offered by plotly (as explained in the documentation Updatemenus documentation).
I tried to play with a code snippet I found in an answer here in stackoverflow (Plotly: How to give different label names in a dropdown menu?).
Here's the modified code:
# imports
import plotly.graph_objects as go
import numpy as np
# data
x = np.linspace(-np.pi, np.pi, 100)
y1 = np.sin(x)
y1b = y1-1
y2 = np.tan(x)
y2b = y2-1
# plotly setup
fig = go.Figure()
# Add one ore more traces
fig.add_traces(go.Scatter(x=x, y=y1,name='sin', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y1b,name='sin - 1', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y2,name='tan', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y2b,name='tan - 1', visible=False, showlegend=True))
# construct menus
updatemenus = [{'active':-1,'buttons': [{'method': 'restyle',
'label': 'Sine',
'args': [{'y': [y1, y1b, y2, y2b],
'label':'Sine',
'name':['sin', 'sin - 1', 'tan', 'tan - 1'],
'visible': True}, [0, 1]],
'args2': [
{'visible': False}, ]
},
{'method': 'restyle',
'label': 'Tan',
'args': [{'y': [y2, y2b, y1, y1b],
'name':['tan', 'tan - 1', 'sin', 'sin - 1'],
'visible':True}, [0, 1]],
'args2': [{
'visible':False},
],
}
],
'direction': 'down',
'showactive': True,}]
# update layout with buttons, and show the figure
fig.update_layout(updatemenus=updatemenus)
fig.show()
This gives me what I wanted to achieve that is to display the sine plots when I first click on Sine in the dropdown menu and clear the plot when I click again on Sine (and the same for Tan).
However, my problems are :
I can't find any information in the documentation or anywhere else on what to pass to args and args2. Could you please provide any reference to documentation ?
At the end, I'm not even sure how to understand the behavior of args especially the first item (dictionary with 'y' and 'name' keys) and the second item with what I understand is a list of trace indices (?). In the following example, I set the list of indices to [2, 3] in the both Sine and Tan buttons. Therefore, I expect to have Tan plots by clicking on the Sine buttons and vice versa for the other button. But the plots do not change.
# construct menus
updatemenus = [{'active':-1,'buttons': [{'method': 'restyle',
'label': 'Sine',
'args': [{'y': [y1, y1b, y2, y2b],
'label':'Sine',
'name':['sin', 'sin - 1', 'tan', 'tan - 1'],
'visible': True}, [2, 3]],
'args2': [
{'visible': False}, ]
},
{'method': 'restyle',
'label': 'Tan',
'args': [{'y': [y2, y2b, y1, y1b],
'name':['tan', 'tan - 1', 'sin', 'sin - 1'],
'visible':True}, [2, 3]],
'args2': [{
'visible':False},
],
}
],
'direction': 'down',
'showactive': True,}]
Could you please show me the correct way to set these values to target the right trace ?
Thank you in advance !


