from plotly.express import scatterfig = scatter(x=incomes, y=expectancies, height=350, title="Life Expectancy by Income", labels={"x": "Income", "y": "Life Expectancy (years)"}, trendline="ols", trendline_color_override="red")fig.show()
FYI
Under the hood, plotly uses the statsmodels package to calculate the trend, so you may have to install that package as well.
In addition to "ols" trend, which is an Ordinary Least Squares linear trend, we can use a "lowess" trend which is a non-parametric method that can be a better fit for non-linear relationships:
from plotly.express import scatterfig = scatter(x=incomes, y=expectancies, height=350, title="Life Expectancy by Income", labels={"x": "Income", "y": "Life Expectancy (years)"}, trendline="lowess", trendline_color_override="red")fig.show()
If you notice, for the lowess trend, there is a slight bend in the curve.