pythonplotly-dash

sketch partial regression chart using dash


i have dataframe, in which we are given x and y columns and between them i want to sketch regression model, main idea is that i should use dash framework, as according chow test , there could be difference between two regression model at different instance value,based on following link : dash models

i wrote following code :

import pandas as pd
from dash import Dash,html,dcc,callback,Output,Input
from sklearn.linear_model import LinearRegression
import plotly.express as px
data =pd.read_csv("regression.csv")
model =LinearRegression()
print(data)
app = Dash()

# Requires Dash 2.17.0 or later
app.layout = [
    html.H1(children='Our regression Model', style={'textAlign':'center'}),
    dcc.Dropdown(data.Year.unique(), '2004', id='dropdown-selection'),
    dcc.Graph(id='graph-content')
]

@callback(
    Output('graph-content', 'figure'),
    Input('dropdown-selection', 'value')
)
def  scatter_graph(value):
    selected =data[data.Year==value]
    return px.scatter(selected,x='x',y='y')
@callback(
    Output('graph-content', 'figure'),
    Input('dropdown-selection', 'value')
)
def  Regression_graph(value):
    selected =data[data.Year==value]
    X =selected['x'].values
    X =X.reshape(-1,1)
    y =selected['y'].values
    model.fit(X,y)
    y_predicted =model.predict(X)
    return px.line(selected,x='x',y=y_predicted)

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

this part works fine :

@callback(
    Output('graph-content', 'figure'),
    Input('dropdown-selection', 'value')
)
def  scatter_graph(value):
    selected =data[data.Year==value]
    return px.scatter(selected,x='x',y='y')

but second decorator for regression plot does not work, here is example : enter image description here

please help me how to fix it?


Solution

  • It may need to add all code in one function and return both figures

        fig = px.scatter(selected, x=x, y=y)
        fig.add_traces(px.line(selected, x=x, y=y_predicted).data)
        return fig
    

    Full working code with random data.

    import pandas as pd
    from dash import Dash,html,dcc,callback,Output,Input
    from sklearn.linear_model import LinearRegression
    import plotly.express as px
    
    from dash.exceptions import PreventUpdate
    
    import random
    random.seed(0)   # to get always the same random values
    data = pd.DataFrame({
                    'x': random.choices(range(0, 100), k=100),
                    'y': random.choices(range(0, 100), k=100),
                    'Year': ['2025']*50 + ['2024']*50,
    })
    
    #data = pd.read_csv("regression.csv")
    model = LinearRegression()
    print(data)
    app = Dash()
    
    # Requires Dash 2.17.0 or later
    app.layout = [
        html.H1(children='Our regression Model', style={'textAlign':'center'}),
        dcc.Dropdown(data.Year.unique(), '2004', id='dropdown-selection'),
        dcc.Graph(id='graph-content')
    ]
    
    @callback(
        Output('graph-content', 'figure'),
        Input('dropdown-selection', 'value')
    )
    def update_graph(value):
        if value is None:
            raise PreventUpdate
    
        print(f'{value = }')
        selected = data[data.Year==value]
    
        x = selected['x'].values
        print(f'{x = }')
        X = x.reshape(-1,1)
        print(f'{X = }')
        y = selected['y'].values
        print(f'{y = }')
        model.fit(X, y)
        y_predicted = model.predict(X)
    
        # fig = px.line(selected, x=x, y=y_predicted)
        # fig.add_scatter(x=X, y=y)  # doesn't show it
    
        fig = px.scatter(selected, x=x, y=y)
        fig.add_traces(px.line(selected, x=x, y=y_predicted).data)
    
        return fig
    
    
    if __name__ =='__main__':
        app.run(debug=True)