用Python中的Plotly以正确的顺序绘制每小时时间序列

时间:2018-01-15 21:29:44

标签: python-3.x plotly

我正在尝试绘制一个时间序列图表,除了时间之外,一切都在工作。出于某种原因,情节是在第一天和之后的情节中绘制,它是以不同的时间和同一时期开始的。通常Plotly应该识别我的时间格式。有谁知道它为什么不能正常工作?

这是我的代码:

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

x = ['2017-01-01 00:00:00','2017-01-02 06:00:00','2017-01-03 12:00:00','2017-01-04 18:00:00','2017-01-05 00:00:00',
 '2017-01-06 06:00:00','2017-01-07 12:00:00','2017-01-08 18:00:00','2017-01-09 00:00:00','2017-01-10 06:00:00',
  '2017-01-01 12:00:00','2017-01-02 18:00:00','2017-01-03 00:00:00','2017-01-04 06:00:00','2017-01-05 12:00:00',
 '2017-01-06 18:00:00','2017-01-07 00:00:00','2017-01-08 06:00:00','2017-01-09 12:00:00','2017-01-10 18:00:00',
  '2017-01-01 00:00:00','2017-01-02 06:00:00','2017-01-03 12:00:00','2017-01-04 18:00:00','2017-01-05 00:00:00',
 '2017-01-06 06:00:00','2017-01-07 12:00:00','2017-01-08 18:00:00','2017-01-09 00:00:00','2017-01-10 06:00:00',
  '2017-01-01 12:00:00','2017-01-02 18:00:00','2017-01-03 00:00:00','2017-01-04 06:00:00','2017-01-05 12:00:00',
 '2017-01-06 18:00:00','2017-01-07 00:00:00','2017-01-08 06:00:00','2017-01-09 12:00:00','2017-01-10 18:00:00']

y1 = np.random.uniform(low=14, high=19.5, size=(40,))
y2 = np.random.uniform(low=14, high=25.0, size=(40,))

trace1 = go.Scatter(
    x=x,
    y=y1,
    name = "Temperature LED",
    line = dict(color = '#17BECF'),
    opacity = 0.8)

trace2 = go.Scatter(
    x=x,
    y=y2,
    name = "Temperature MDL",
    line = dict(color = '#7F7F7F'),
    opacity = 0.8)

data = [trace1,trace2]

layout = dict(
    title='Temperature LED vs MDL',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1d',
                     step='day',
                     stepmode='backward'),
                dict(count=6,
                     label='6d',
                     step='day',
                     stepmode='backward'),
                dict(step='all')
            ])
        ),
    rangeslider=dict(),
    type='date'
    )
)

fig = dict(data=data, layout=layout)

py.plot(fig, filename="Time Series with Rangeslider")

这是结果: Plotly Result

1 个答案:

答案 0 :(得分:1)

在绘制轨迹之前,Plotly不会对轨迹的x值重新排序,因此将它们按顺序绘制会产生来回跳跃的线条。在创建跟踪之前,对x值进行排序(在这种情况下,您可以通过调用x.sort()进行排序)来修复问题:

enter image description here

请注意,此图中的所有垂直线都是x列表中包含两次的所有时间的结果(我不确定这是否是故意的)。