简体   繁体   English

Plotly:如何使用 plotly express 显示时间序列数据的趋势线?

[英]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.使用 plotly express 中内置的“提示”dataframe,我首先创建了一个日期时间列。

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:使用一列日期时间作为 x 轴会产生错误:

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.最安全的方法是对日期的序列化表示运行回归,然后将 x 轴设置为显示为字符串。 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 .通过序列化表示,我的意思是,例如,Ben.T 在他的评论中建议的方法,或者 Plot 中使用的最适合 plotly 的方法 Then you can set up the layout of the x-axis using:然后您可以使用以下命令设置 x 轴的布局:

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. df_tips['date'][0::40]部分确保每个刻度线之间有一些空间。

Plot 1: 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') :如果您想使用数据集的其他维度,这种方法甚至效果很好,例如: fig = px.scatter(df_tips,x='date',y='tip', color = 'sex', trendline='ols')

Plot 2: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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