简体   繁体   中英

Plotly: How to show trendline for time series data using plotly express?

Using the built in "tips" dataframe in plotly express, I first create a datetime column.

import plotly.express as px
import pandas as pd
from datetime import datetime

df_tips = px.data.tips()
datelist = pd.date_range(datetime.today(), periods=df_tips.shape[0]).tolist()
df_tips['date'] = datelist

Using a column of datetimes as the x-axis gives the error:

px.scatter(df_tips,x='date',y='tip',trendline='ols')    
...
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]

Using any other column does not. Is there a good way to do this?

The safest approach is to run the regression on a serialized representation of your dates, and then set th x-axis up to be displayed as strings. By serialized representation I mean, for example, the approach suggested by Ben.T in his comment, or the one used in Plot best fit line with plotly . Then you can set up the layout of the x-axis using:

fig.update_xaxes(tickangle=45,
                 tickmode = 'array',
                 tickvals = df_tips['date'][0::40],
                 ticktext= [d.strftime('%Y-%m-%d') for d in datelist[0::40]])

The df_tips['date'][0::40] part makes sure there's some space between each tickmark.

Plot 1:

在此处输入图像描述

This approach even works well if you'd like to make use of other dimensions of your dataset with, for example: fig = px.scatter(df_tips,x='date',y='tip', color = 'sex', trendline='ols') :

Plot 2:

在此处输入图像描述

Complete code:

import plotly.express as px
import pandas as pd
from datetime import datetime

df_tips = px.data.tips()
df_tips['date'] = df_tips.index
datelist = pd.date_range(datetime.today(), periods=df_tips.shape[0]).tolist()

fig = px.scatter(df_tips,x='date',y='tip', trendline='ols')
fig = px.scatter(df_tips,x='date',y='tip', color = 'sex', trendline='ols')  

fig.update_xaxes(tickangle=45,
                 tickmode = 'array',
                 tickvals = df_tips['date'][0::40],
                 ticktext= [d.strftime('%Y-%m-%d') for d in datelist])

fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM