Black Lives Matter. Please consider donating to Black Girls Code today.

New yaxis per selected df column

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()

You could use string formatting, like write yaxis="y%d" %i (if channels start at 0 then you need to use %(i+1) since axes names start at 1). Also see https://plot.ly/python/multiple-axes/#multiple-axes to see how to control the parameters of your multiple y axes (position etc.)

I tried this, the first axis will appear but it won’t add any more when selecting more traces;

def updateGraph(channels, test):
    if channels:
        j=1
        my_layout = {}
        my_axis = list("")
        for index, column in enumerate(list(channels)):
            my_layout['yaxis' + str(j)] = {}
            my_layout['yaxis' + str(j)]['title'] = column
            my_axis.append('y' + str(j))
            j=index+1

            return go.Figure(data=[go.Scatter(x=dict_main[test].index, y=dict_main[test][i], name=i, yaxis="y%d" %(j)) for i in channels],layout=my_layout)

I also tried to populate ‘my_axis’ with ‘y1,y2,y3’ etc and then say yaxis=my_axis but this resulted in the same problem.