简体   繁体   English

如何用 plotly 可视化时间序列?

[英]How to visualize time series with plotly?

I have a data set that contains two variables: clicks (object) and session_time (datetime64[ns]) .我有一个包含两个变量的数据集: clicks (object)session_time (datetime64[ns])

I am trying to plot a time series by using plotly.我正在尝试使用 plotly 绘制时间序列。 But the output looks pretty weird and I could not find the reason.但是输出看起来很奇怪,我找不到原因。

Here is the code that I am trying to plot it:这是我试图绘制它的代码:

import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as numpy

trace = go.Scatter(x=list(df.session_time),
                   y=list(df.clicks), line=dict(color='red'))

dat = [trace]
layout = dict(
    title='Clicks on books',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(step='all')
            ])
        ),
        rangeslider=dict(
            visible = True
        ),
        type='date'
    )
)

fig = dict(data=dat, layout=layout)
pyo.iplot(fig)

And here the output is:这里的输出是: 在此处输入图片说明

Any idea what I am missing?知道我缺少什么吗?

go.Scatter is plotting your data as a scatterplot, and since your datetime data is out of order, it's plotting it out of order. go.Scatter将您的数据绘制为散点图,并且由于您的日期时间数据乱序,因此它乱序绘制。 After converting your session_time column to datetime, sorting it bysession_time列转换为 datetime 后,按以下方式对其进行排序

df.sort_values(by=['session_time'], inplace=True)

will resolve your issue.将解决您的问题。 This may not be the most elegant solution, but it works.这可能不是最优雅的解决方案,但它有效。

排序日期时间

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

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