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:

        'data': [
                x=df[df['SpeedCoefficient'] == i]['SpeedCoefficient'],
                y=df[df['SpeedCoefficient'] == i]['Km/h'],
                text=df[df['SpeedCoefficient'] == i]['Subproject'],
                    '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
            ) 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},
            font={'color': 'white'},

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


Hi @jonny-sexton welcome to the forum! You can add automatically regression lines from least-squares optimization in px.scatter: see 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 For the value of the error bars you can take a look at the model parameters, see

1 Like

@Emmanuelle thanks for the suggestion on @jonny-sexton’s question! I’m trying something similar myself, but I’m having a tough time identifying the confidence interval error values.

@jonny-sexton were you able to make this work? If so, would you be willing to share some tips or example solution?