It seems that using the Trend line in Plotly requires that the ‘statsmodels’ package be installed to work.

Simply download the package to your current environment using pip install statsmodels or conda install anaconda::statsmodels if you’re using Anaconda and rerun your code.

when you use the ‘trendline’ argument in px.scatter(), Plotly will use this library in the background to compute the trend line, hence it must be available within your environment in order for Plotly to make a call to the library.

This is the approach I use for trendlines or best fit:

Calculate y-intercept and slope using scipy.stats.lingress

get the minimum and maximum x-coordinate values from your data

calculate the y-values associated with min and max x

do a 2-point scatter plot with mode = ‘lines’, to connect [xmin, (slopexmin)+y-intercept] and [xmax, (slopexmax)+y-intercept]
This approach is very simple, hope it helps you.