简体   繁体   English

如何使用 plotly 制作交互式时间序列 plot?

[英]How to make an interactive time serie plot using plotly?

I am trying to make an interactive time serie visualization using plotly and jupyter notebook.我正在尝试使用 plotly 和 jupyter notebook 进行交互式时间序列可视化。 I want to have a simple plot where I can filter the index of a dataframe using plotly and ipywidget and store the new index I have.我想要一个简单的 plot ,我可以在其中使用 plotly 和 ipywidget 过滤 dataframe 的索引并存储我拥有的新索引。 But, I have no idea how to do so.但是,我不知道该怎么做。 I am investigating the documentation without any success.我正在调查文档但没有任何成功。 What I am doing so far:到目前为止我在做什么:

import pandas as pd
import numpy as np
import plotly.graph_objs as go
from ipywidgets import interactive

index = pd.date_range(start='2020-01-01', end='2020-01-15', freq='D')
timeserie = pd.DataFrame(np.random.normal(0,1,size=index.size), index=index, columns=['sensor'])
fig = go.FigureWidget([
    go.Scatter(
        x=timeserie.index.values,
        y=timeserie.values,
        mode='markers'
    )
])
 
def update_training_dataset(index_min, index_max, sensor):
    scatter = fig.data[0]
    index = timeserie.loc[(timeserie.index >= index_min) & (timeserie.index <= index_max)].index
    sensor_value = timeserie.loc[scatter.x, sensor].values
    with fig.batch_update():
        fig.layout.yaxis.title = sensor
        scatter.x = index
        scatter.y = sensor_value
 
interactive(update_training_dataset, index_min=index, index_max=index, sensor=timeserie.columns)

But, it leads to a strange error.. KeyError: "None of [Int64Index([15778368000000000000, ... are in the [index]" This is weird as the index of my timeserie has datetimeindex as type. This code would lead to updating the dataframe according to the values of sensor, index_min, index_max that the user set. Also, I note that the date are provided in a select widget... I would love to have a date picker here. Can someone help me? Provide any code that I can get some insights from? Thank you:)但是,它会导致一个奇怪的错误.. KeyError: "None of [Int64Index([15778368000000000000, ... are in the [index]" 这很奇怪,因为我的时间序列的索引有 datetimeindex 作为类型。这段代码会导致根据用户设置的传感器、index_min、index_max 的值更新 dataframe。此外,我注意到日期是在 select 小部件中提供的......我很想在这里有一个日期选择器。有人可以帮我吗?提供我可以从中获得一些见解的任何代码吗?谢谢:)

EDIT编辑

The solution is provided below thanks to Serge:)感谢 Serge,下面提供了解决方案:)

fig = go.FigureWidget([
    go.Scatter(
        x=timeserie.index,
        y=timeserie.values,
        mode='markers'
    )
])
 
def update_training_dataset(index_min, index_max, Sensor):
    scatter = fig.data[0]
    index = timeserie.loc[(timeserie.index >= index_min) & (timeserie.index <= index_max)].index
    sensor_value = timeserie.loc[scatter.x, Sensor].values
    with fig.batch_update():
        fig.layout.yaxis.title = Sensor
        scatter.x = index
        scatter.y = sensor_value
 
        
date_picker_max = DatePicker(
    description='End date',
    disabled=False,
    value = index.max()
)
 
date_picker_min = DatePicker(
    description='Start date',
    disabled=False,
    value = index.min()
)
 
interact(
    update_training_dataset, 
    index_min=date_picker_min, 
    index_max=date_picker_max, 
    Sensor=timeserie.columns
)

I am still working on a way to have hours:minutes:seconds in the date picker.我仍在努力在日期选择器中设置小时:分钟:秒。

EDIT 2 By the way, no need to use interact instead of interactive: they seem to support widgets as parameters.编辑 2顺便说一句,不需要使用交互而不是交互:它们似乎支持小部件作为参数。 Also, you need to import ipydatetime as below to get datetime picker.此外,您需要按如下方式导入 ipydatetime 以获取日期时间选择器。

# usual imports
from ipydatetime import DatetimePicker

fig = go.FigureWidget([
    go.Scatter(
        x=timeserie.index,
        y=timeserie.values,
        mode='markers'
    )
])
 
def update_training_dataset(index_min, index_max, Sensor):
    scatter = fig.data[0]
    index = timeserie.loc[(timeserie.index >= index_min) & (timeserie.index <= index_max)].index
    sensor_value = timeserie.loc[scatter.x, Sensor].values
    with fig.batch_update():
        fig.layout.yaxis.title = Sensor
        scatter.x = index
        scatter.y = sensor_value
 
        
date_picker_max = DatetimePicker(
    description='End date',
    disabled=False,
    value = index.max()
)
 
date_picker_min = DatetimePicker(
    description='Start date',
    disabled=False,
    value = index.min()
)
 
interact(
    update_training_dataset, 
    index_min=date_picker_min, 
    index_max=date_picker_max, 
    Sensor=timeserie.columns
)

Actually, your code is all good.实际上,您的代码都很好。 You did a simple mistake in the definition of fig .您在fig的定义中犯了一个简单的错误。 Try the following尝试以下

fig = go.FigureWidget([
    go.Scatter(
        x=timeserie.index,
        y=timeserie.values,
        mode='markers'
    )
])
 
def update_training_dataset(index_min, index_max, sensor):
    scatter = fig.data[0]
    index = timeserie.loc[(timeserie.index >= index_min) & (timeserie.index <= index_max)].index
    sensor_value = timeserie.loc[scatter.x, sensor].values
    with fig.batch_update():
        fig.layout.yaxis.title = sensor
        scatter.x = index
        scatter.y = sensor_value
 
interactive(update_training_dataset, index_min=index, index_max=index, sensor=timeserie.columns)

You simly made the error of defining x=timeserie.index.values when it actually should be x=timeserie.index .当它实际上应该是x=timeserie.index timeserie.index 时,您只是犯了定义x=timeserie.index.values的错误。

The result is fine when this is changed.更改后结果很好。

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

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