# Displaying 2 trendlines for 1 set of data with Plotly

**URL:** <https://community.plotly.com/t/displaying-2-trendlines-for-1-set-of-data-with-plotly/68972>\
**Category:** 📊 Plotly Python\
**Tags:** question\
**Created:** [October 17, 2022, 12:57pm UTC](https://community.plotly.com/t/displaying-2-trendlines-for-1-set-of-data-with-plotly/68972 "2022-10-17T12:57:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurahendrickx](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@laurahendrickx](https://community.plotly.com/u/laurahendrickx)\
**Post date:** [October 17, 2022, 12:57pm UTC](https://community.plotly.com/t/displaying-2-trendlines-for-1-set-of-data-with-plotly/68972/1 "2022-10-17T12:57:26Z")

</div>

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 🙏!

---

<div class="post-metadata">

**Author:** ![payton-dev](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@payton-dev](https://community.plotly.com/u/payton-dev)\
**Post date:** [October 18, 2022, 12:34am UTC](https://community.plotly.com/t/displaying-2-trendlines-for-1-set-of-data-with-plotly/68972/2 "2022-10-18T00:34:13Z")

</div>

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

```auto
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.

```auto
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

```auto
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

```auto
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.

```auto
for traces in fig_trace:
    fig.append_trace(traces, row=1, col=1)

```

Combined together you get:

```auto
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.

---

<div class="post-metadata">

**Author:** ![laurahendrickx](https://avatars.discourse-cdn.com/v4/letter/l/b5a626/32.png) [@laurahendrickx](https://community.plotly.com/u/laurahendrickx)\
**Post date:** [October 27, 2022, 7:02am UTC](https://community.plotly.com/t/displaying-2-trendlines-for-1-set-of-data-with-plotly/68972/3 "2022-10-27T07:02:31Z")

</div>

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 😉
