As far as I know the trendline function only works with plotly express, if you want to try and use graph objects you need to create your own with statesmodel (or what ever else you’d like to use) then graph those data points.
Someone else may have a more elegant solution for you but this is an easy way about doing what you need. What you could do is create two of the same px.scatter graphs with one using trendline=‘ols’ and one trendline=‘lowess’ then add the traces together to create a single plot.
First we will need to make a sub plot with go.Figure and make_subplots
from plotly.subplots import make_subplots
fig = go.Figure(make_subplots(rows=1, cols=1))
Next make your figure two times with the different trendline options. Note that the figure names are different than above.
fig1 = px.scatter(df, x='Sales', y='KG', trendline="ols")
fig2 = px.scatter(df, x='Sales', y='KG', trendline="lowess")
Next we will create a blank list
fig_trace = []
Then we will do for trace in range to append the trace data to the fig_trace list, repeated twice for the fig1 and fig2 created above
for trace in range(len(fig1["data"])):
fig_trace.append(fig1["data"][trace])
for trace in range(len(fig2["data"])):
fig_trace.append(fig2["data"][trace])
Lastly we will do for traces in fig_trace to append the list we just created to the go.figure subplot. I couldn’t get it to workout without using the subplot but we will place both traces in the same subplot making a single plot.
for traces in fig_trace:
fig.append_trace(traces, row=1, col=1)
Combined together you get:
from plotly.subplots import make_subplots
fig = go.Figure(make_subplots(rows=1, cols=1))
fig1 = px.scatter(df, x='Sales', y='KG', trendline="ols")
fig2 = px.scatter(df, x='Sales', y='KG', trendline="lowess")
fig_trace = []
for trace in range(len(fig1["data"])):
fig_trace.append(fig1["data"][trace])
for trace in range(len(fig2["data"])):
fig_trace.append(fig2["data"][trace])
for traces in fig_trace:
fig.append_trace(traces, row=1, col=1)
fig.show()
Essentially we are just making to exact same plots with the only difference being the trendline then we are adding them together as traces.