Bojan Pavlović
Bojan Pavlović

Reputation: 113

Connecting graphs with dropdown (Plotly Dash)

I am building the app in which I want to plot two separate graphs from two data frames. I would like to use the dropdown to display only one graph per page based on each data frame (df,df1). I followed the Plotly reference ​and couldn't reproduce it on my example. This is my code so far that works (it displays both graphs on the same page, without dropdown):

import pandas as pd
import dash
import plotly.express as px
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)

data = [['Blue',20],['Red ',12],['Green',33]]
df = pd.DataFrame(data,columns=['Color','Number'])

data1 = [['A',10,88],['B ',50,45],['C',25,120]]
df1 = pd.DataFrame(data1,columns=['Letter','Column1','Column2'])

fig = px.bar(df, x=df['Color'], y=df['Number'])
fig1 = px.line(x=df1['Letter'], y=df1['Column1'], color=px.Constant('Column1'),
             labels=dict(x='Letter', y='Column1', color='Letter'))
fig1.add_bar(x=df1['Letter'], y=df1['Column2'], name='Letter')

app.layout = html.Div(children=[
    html.H1(children='Colors and Letters', style={'text-align': 'center'}),

     html.Div(children='Color', style={'text-align': 'center'}),

    dcc.Graph(
        id='example-graph',
        figure=fig
    ),
    html.Div(children='Letter', style={'text-align': 'center'}),

    dcc.Graph(
        id='example-graph1',
        figure=fig1
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

This is what I would like to get:

enter image description here

What would be the best approach to do this? Thanks in advance.

Upvotes: 1

Views: 4686

Answers (1)

r-beginners
r-beginners

Reputation: 35135

I don't have much experience with Dash, but here's a great answer and your code throughout. The point is to introduce a dropdown so that the initial value is displayed as fig. Depending on the return value of the callback function, I use the if function to switch the graph.

import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

data = [['Blue',20],['Red ',12],['Green',33]]
df = pd.DataFrame(data,columns=['Color','Number'])

data1 = [['A',10,88],['B ',50,45],['C',25,120]]
df1 = pd.DataFrame(data1,columns=['Letter','Column1','Column2'])

app.layout = html.Div(children=[
    html.H1(children='Colors and Letters', style={'text-align': 'center'}),
    html.Div(children='Color', style={'text-align': 'center'}),

    html.Div([
        html.Label(['Choose a graph:'],style={'font-weight': 'bold'}),
        dcc.Dropdown(
            id='dropdown',
            options=[
                {'label': 'graph1', 'value': 'graph1'},
                {'label': 'graph2', 'value': 'graph2'},
                    ],
            value='graph1',
            style={"width": "60%"}),
        
    html.Div(dcc.Graph(id='graph')),        
        ]),

])

@app.callback(
    Output('graph', 'figure'),
    [Input(component_id='dropdown', component_property='value')]
)
def select_graph(value):
    if value == 'graph1':
        fig = px.bar(df, x=df['Color'], y=df['Number'])
        return fig
    else:
        fig1 = px.line(x=df1['Letter'], y=df1['Column1'], color=px.Constant('Column1'),
                     labels=dict(x='Letter', y='Column1', color='Letter'))
        fig1.add_bar(x=df1['Letter'], y=df1['Column2'], name='Letter')
        return fig1
    
if __name__ == '__main__':
    app.run_server(debug=True)

enter image description here

Upvotes: 2

Related Questions