Search code examples
plotlyregressiontrendline

plotly trendline that doesn't make sense


Trying to map a trendline in plotly for the first time, and I'm getting results that don't make sense. First, the line isn't straight (it's overfitted, but in a way that doesn't make sense). Second, even if the line were straightened out, its angle doesn't make sense.

Clearly, I'm doing something wrong, and I suspect it's simple/fundamental . . . but I can't spot it. Thanks in advance!

plotly scatter graph, with nonsensical and nonlinear trendline

import pandas as pd
import plotly.express as px

df=pd.DataFrame()
df['month'] = pd.to_datetime(['2020-04-01','2020-05-01','2020-06-01','2020-07-01','2020-08-01','2020-09-01','2020-10-01','2020-11-01','2020-12-01','2021-01-01','2021-02-01','2021-03-01'])
df['views']=[7942, 114511, 192292, 230733, 272823, 271613, 274803, 227554, 270407, 284185, 307765, 329814]

fig= px.scatter(
    df, 
    x='month', 
    y='views', 
    opacity=1,
    trendline='ols',
    trendline_scope='overall'
)
fig.show()

Solution

    • check versions of plotly and statsmodels
    • works with plotly 5.2.1 and statsmodels 0.12.2
    import plotly
    import statsmodels
    
    print(plotly.__version__, statsmodels.__version__)