✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

# Adding best fit line (linear regression line) in a scatter plot

I have been using Plotly lately for its great interactive features. I am wondering if I can generate a correlation scatter plot with best line fit, as I used to do in seaborn, using `regplot()` method (just like the following figure)?

I checked the attributes of `go.Scatter()` object in the API libraries, but I could not find it

What would be a simple way to add the best line fit? Any idea?

Thanks a lot!

@mealbahrani
Here https://plot.ly/python/linear-fits/ is an example of plotting the linear fit.
If you want a plot similar to that generated by `seaborn.regplot`, simply define:

``````rg=sns.regplot(your data)
``````

and extract from `rg` the regression line data and the Path(s) bounding the confidence interval band, to plot them via Plotly:

``````X=rg.get_lines()[0].get_xdata()# x-coordinate of points along the regression line
Y=rg.get_lines()[0].get_ydata()# y-coordinate
P=rg.get_children()[1].get_paths()#The list of Path(s) bounding the shape of 95% confidence interval-transparent
``````

Define two traces of type `scatter`: one for your data points and the second for the regression line, defined by X and Y.
To get the transparent confidence interval band along the regression line, extract the path describing the boundary of that band as follows:

``````p_codes={1:'M', 2: 'L', 79: 'Z'}#dict to get the Plotly codes for commands to define the svg path
path=''
for s in P[0].iter_segments():
c=p_codes[s[1]]
xx, yy=s[0]
path+=c+str('{:.5f}'.format(xx))+' '+str('{:.5f}'.format(yy))

shapes=[dict(type='path',
path=path,
line=dict(width=0.1,color='rgba(68, 122, 219, 0.25)' ),
fillcolor='rgba(68, 122, 219, 0.25)')]
layout['shapes']=shapes
``````

The resulting figure looks like this one:
https://plot.ly/~empet/14628

2 Likes