I have a basic dash app that graphs some data from a dictionary of dataframes. The first dropdown selects the df, while the second selects the columns of the df to be plotted.
This works well, but I can’t seem to add a new yaxis for each of the plotted columns. First, I tried to change the updateGraph callback to include yaxis=i
after defining x, y & name. Looking at the documentation, it seems that I can define the yaxis in go.Scatter
but that I would need to set them as ‘y2’, 'y3, ‘y4’ etc. I’ve also tried to update the layout via go.Figure.add_trace
in this way but neither has worked. The code is below, where dict_main
is a dictionary of dataframes of various sizes.
All help is appreciated!
data = list(dict_main.keys())
channels = dict_main[data[0]]
app.layout = html.Div(
[
html.Div([
dcc.Dropdown(
id='data-dropdown',
options=[{'label': speed, 'value': speed} for speed in data],
value=list(dict_main.keys())[0],
searchable=False
),
], style={'width': '49%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='channel-dropdown',
multi=True
),
], style={'width': '49%', 'display': 'inline-block'}
),
html.Div([
dcc.Graph(
id='Main-Graph',
),
], style={'width': '98%', 'display': 'inline-block'}
)
]
)
@app.callback(
Output('channel-dropdown', 'options'),
[Input('data-dropdown', 'value')])
def update_date_dropdown(speed):
return [{'label': i, 'value': i} for i in dict_main[speed]]
@app.callback(
Output('Main-Graph', 'figure'),
[Input('channel-dropdown', 'value')],
[State('data-dropdown', 'value')])
def updateGraph(channels, speed):
if channels:
return go.Figure(data=[go.Scatter(x=dict_main[speed].index, y=dict_main[speed][i], name=i, yaxis='y2') for i in channels])
else:
return go.Figure(data=[])
if __name__ == '__main__':
app.run_server()