Adding a linear regression to a go.Scatter() trace


(I know one answer to this question is to re-arrange my data into a tidy-er format and then use plotly express, but assuming that I have to stick with go.Scatter()…)

Is there a way I can add a regression trend line to the red points in this plot? Basically I want to see if a regression line is above or below the blue line.

I found this answer but it seems a little out of date: Adding best fit line (linear regression line) in a scatter plot . I guess more specifically, I don’t understand what I do with layout['shapes']=shapes in the accepted answer.

Actually as of 4.8 you don’t have to reformat your data to be tidy to use Plotly Express! See for more details.

That said, if you want to do things manually, here’s the code that Plotly Express uses internally, which you can run yourself if you like:

Basically you pass in x and y to statsmodels to get a “predicted” y column and you add a second go.Scatter() trace with the original x and the predicted y.

1 Like