[Python] Dash Plotly X-Axis

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dash plot graph (see below link) that shows the "Inspection Date" on the x-axis and the "SCORE" on the y-axis. My goal is to be able to switch (with radio buttons) the y-axis between the "SCORE" and "Points" (please see below picture of the dateframe). As you can see in the code, I added an input (dcc.RadioItems) with the ID 'radio' in the layout and in the callback function. I named it "argu1" in the function.

    My below code works as intended if I leave the y-axis as "SCORE", but the graph will only shows me the average "score".

    fig = px.line(dff, x="InspecDate", y='SCORE', color='CUISINE', height=600)


    If I change the y-axis to the argument I assigned it to (argu1),

    fig = px.line(dff, x="InspecDate", y=argu1, color='CUISINE', height=600)


    I get this error message: ValueError: Value of 'y' is not the name of a column in 'data_frame'. Expected one of ['InspecDate', 'CUISINE', 'Points', 'SCORE'] but received: Score

    Can someone please help why I am getting this error message? Your help is greatly appreciated.

    Graph: [​IMG]

    Dataframe:

    [​IMG]

    My code:

    app = dash.Dash(__name__)

    app.layout = html.Div([

    dcc.Graph(id='our_graph'),

    dcc.Dropdown(id='cuisine_one',
    options=[{'label':x, 'value':x} for x in df.sort_values('CUISINE')['CUISINE'].unique()],
    value='African'),

    dcc.Dropdown(id='cuisine_two',
    options=[{'label':x, 'value':x} for x in df.sort_values('CUISINE')['CUISINE'].unique()],
    value='Asian'),

    dcc.Dropdown(id='cuisine_three',
    options=[{'label':x, 'value':x} for x in df.sort_values('CUISINE')['CUISINE'].unique()],
    value='Donuts'),
    html.Br(),

    dcc.RadioItems(id='radio',
    options={
    'SCORE' : 'avg_score',
    'Points': 'avg_points'},
    value='Score') ])

    @app.callback(
    Output('our_graph','figure'),
    Input('cuisine_one','value'),
    Input('cuisine_two','value'),
    Input('cuisine_three','value'),
    Input('radio', 'value') )

    def build_graph(first_cuisine, second_cuisine, third_cuisine, argu1):
    dff=df[(df['CUISINE']==first_cuisine)|
    (df['CUISINE']==second_cuisine)|
    (df['CUISINE']==third_cuisine)]

    fig = px.line(dff, x="InspecDate", y='SCORE', color='CUISINE', height=600)

    return fig

    if __name__ == '__main__':
    app.run_server(debug=False, port = 8060)

    Continue reading...

Compartilhe esta Página