Plotly Express Lowess Trendline Produces Incorrect Results

I have recently implemented a LOWESS trendline using Plotly express when I noticed it didn’t look quite right.

Replicating the example from the docs does not appear to produce the trendline as shown in the screenshot.

If we do this manually using LOWESS directly from statsmodels and then plotting it using Plotly we get the trendline that matches the screenshot in the docs. Green is the manually produced trendline. Blue is the Plotly express LOWESS trend line. They both use a fraction of 0.1.

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import statsmodels.api as sm

df = px.data.stocks(datetimes=True)
lowess = sm.nonparametric.lowess
z = lowess(df['GOOG'],df['date'],frac=0.1)

fig = px.scatter(df, x="date", y="GOOG", trendline="lowess", trendline_options=dict(frac=0.1))

fig.add_trace(go.Scatter(
    x=pd.to_datetime(z[:,0],unit='ns'),
    y=z[:,1], 
    mode='lines',
    name='manual'
))
fig.show()

Any idea what’s going on here or if I am doing something wrong?

Plotly 5.4.0
statsmodels 0.13.1
Python 3.7.11

1 Like

I think it works well…

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import statsmodels.api as sm

df = px.data.stocks(datetimes=True)
lowess = sm.nonparametric.lowess
z = lowess(df['GOOG'],df['date'],frac=0.1)

fig = px.scatter(df, x="date", y="GOOG", trendline="lowess", trendline_options=dict(frac=0.1))

fig.add_trace(go.Scatter(
    x=pd.to_datetime(z[:,0],unit='ns'),
    y=z[:,1], 
    mode='lines',
    name='manual',
    opacity=0.5,
    line=dict(width=13)
))
fig.show()