'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!
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 1:[1]
- 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__)
Solution 2:[2]
Add the following code before making the figure:
df['month'] = (df['month'] -ps.Timestamp(ā1970-01-01ā)) // pd.Timedelta(ā1sā)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rob Raymond |
Solution 2 | Ethan |