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

Adding regression line and confidence interval to a scatter plot

Hi all,

I am currently working on a Data Visualization project:

Is there any built-in function that enables me to automatically plot Regression lines and confidence intervals to this plot, or would I have to compute this manually from the scatter plot data and then plot this to the same graph? Checking the documentation I couldn’t find anything that supports this.

For reference, here is the code I am using for one of the plots:

dcc.Graph(
    id='km/h-speedcoefficient',
    figure={
        'data': [
            dict(
                x=df[df['SpeedCoefficient'] == i]['SpeedCoefficient'],
                y=df[df['SpeedCoefficient'] == i]['Km/h'],
                text=df[df['SpeedCoefficient'] == i]['Subproject'],
                mode='markers',
                opacity=0.7,
                marker={
                    'size': 15,
                    'line': {'width': 0.5, 'color': 'white'},
                    'color': df[df['SpeedCoefficient'] == i]['SpeedCoefficient'],
                    'cmin': min(df['SpeedCoefficient']),
                    'cmax': max(df['SpeedCoefficient']),
                    'colorscale': [[0.0, '#01cdfe'], [1.0, '#ff71ce']],#"Bluered",
                    'autocolorscale': False,
                    'showscale': True
                },
                name=i
            ) for i in df.SpeedCoefficient.unique()
        ],
        'layout': dict(
            title={'text': 'Km/h per SpeedCoefficient (0.1, 0.5, 1.0)'},
            xaxis={'type': 'lin', 'title': 'Speed Coefficient', 'color': 'white'},
            yaxis={'title': 'Km/h', 'color': 'white'},
            margin={'l': 40, 'b': 40, 't': 100, 'r': 10},
            legend={'x': 0, 'y': 1},
            showlegend=False,
            hovermode='closest',
            font={'color': 'white'},
            paper_bgcolor='#303030',
            plot_bgcolor='#424242'
        )
    }
)

Any help would be greatly appreciated, many thanks in advance!

Jonny

Hi @jonny-sexton welcome to the forum! You can add automatically regression lines from least-squares optimization in px.scatter: see https://plotly.com/python/linear-fits/ for examples. You can pass the resulting figure objects to the figure attribute of your dcc.Graph. Confidence intervals are not automatically added, but you can use fig.update_traces to add error bars to the trendline trace, see for example https://plotly.com/python/error-bars/#basic-symmetric-error-bars. For the value of the error bars you can take a look at the model parameters, see https://plotly.com/python/linear-fits/#fitting-multiple-lines-and-retrieving-the-model-parameters

1 Like