✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

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 https://medium.com/plotly/beyond-tidy-plotly-express-now-accepts-wide-form-and-mixed-form-data-bdc3e054f891 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: https://github.com/plotly/plotly.py/blob/7e1ee0ba29ebea5f947f81d5cddc1bdd86b08cb7/packages/python/plotly/plotly/express/_core.py#L288

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