Hi there,
Can anyone tell me how to dynamically show/hide a secondary yaxis when button is changed. Iโve created a graph that graphs 2 different plots depending on the selected button. I want the secondary yaxis to be displayed on the first graph, but when I switch to Yearly I want y2 to be removed. The showticklabels:False argument was a workaround I thought would work. Anyone know how to do this?
# Create figure
fig = go.Figure()
# Add traces
fig.add_trace(
go.Bar(x=list(df_q.index),
y=list(df_q['Total']),
name="Total Number",
visible=True)
)
fig.add_trace(
go.Scatter(x=list(df_q.index),
y=list(df_q['Performance (%)']),
name="Performance",
visible=True,
mode="lines+markers",
line=dict(color="#ffc000")))
fig.add_trace(
go.Scatter(x=list(df_q.index),
y=df_q['Performanc Target'],
name="Target",
visible=True,
line=dict(color="red", dash="dash")))
fig.add_trace(
go.Bar(x=list(df_year.index),
y=list(df_year['Performance (%)']),
name="Performance",
visible=False,
marker_color='lightsalmon'))
fig.add_trace(
go.Bar(x=list(df_year.index),
y=list(df_year['Performanc Target']),
name="Target",
visible=False,
marker_color='indianred',
))
# Add dropdown
fig.update_layout(
updatemenus=[
go.layout.Updatemenu(
active=0,
buttons=list([
dict(method='update',
args=[{"visible": [True, True, True, False, False, False]},
{'yaxis':{'ticksuffix': None}}],
label="Quarterly"
),
dict(method='update',
args=[{"visible": [False, False, False, True, True, True]},
{'yaxis':{'ticksuffix': '%'}},
{'yaxis2':{'showticklabels': False}}],
label="Yearly"
)
]),
direction="down",
pad={"l": 0, "t": -10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
),
]
)
fig.update_layout(
showlegend=False
)
fig['data'][1]['yaxis'] = 'y2'
fig['data'][2]['yaxis'] = 'y2'
fig['layout']['yaxis2'] = {'anchor': 'free',
'overlaying': 'y',
'position': 1.0,
'side': 'right',
'ticksuffix': '%'}