Displaying 2 trendlines for 1 set of data with Plotly

Hi everyone,

I am investigating how a certain dataset changes over time, so I made a scatterplot in plotly.express and added a trendline.

To determine the best fit, I experimented with several trend models. My first fit was linear (trendline = “ols”, second one non-linear, trendline = “lowess”).

Is there a way I could display both trendlines in the same plot? I don´t think it would be possible with plotly.px (please correct me if I´m wrong), so figured I could switch to plotly.go and use traces.

But how could I do this? I´ve only used traces to add differents sets of data, and combine those multiple sets with a trendline each. Now I would like one set of data, with its 2 trendlines.

Thanks so much :pray:!

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.

1 Like

Hi @payton-dev, thank you so much for your elaborate answer!

I will try this one out in my notebook, it will make it look much better I’m sure! I wouldn’t have found this on my own :wink: