Plotly:如何使用最新值直到新值可用来处理时间序列中的缺失值?

我有两个熊猫系列。一个有所有日期的条目,一个有零星的条目。


在下面的示例中绘制df2['Actual']时。在每个时间点绘制最新值而不是在每个记录点之间画一条线的最佳方法是什么。在此示例中,该Actuals线将绘制在 y 轴上的 90 处,直到 2020 年 6 月 3 日将跳至 280。


import pandas as pd

import plotly.graph_objs as go


d1 = {'Index': [1, 2, 3, 4, 5, 6],

     'Time': ["2020-06-01", "2020-06-02", "2020-06-03", "2020-06-04" ,"2020-06-05" ,"2020-06-06"],

     'Pred': [100, -200, 300, -400 , -500, 600]

    }


d2 = {'Index': [1, 2, 3],

     'Time': ["2020-06-01", "2020-06-03","2020-06-06"],

     'Actual': [90, 280, 650]

    }

df1 = pd.DataFrame(data=d1)

df2 = pd.DataFrame(data=d2)


def plot_over_time(df1, df2):

    fig = go.Figure()

    traces = []

    fig.add_trace(dict(

        x=df1['Time'], y=df1['Pred'],

        mode='lines+markers',

        marker=dict(size=10),

        name = "Preds"))    

    fig.add_trace(dict(

        x=df2['Time'], y=df2['Actual'],

        mode='lines+markers',

        marker=dict(size=10),

        name = "Actuals"))

    fig.show()


plot_over_time(df1, df2)

http://img3.mukewang.com/63b538de000139e706270341.jpg

幕布斯7119047
浏览 85回答 1
1回答

弑天下

使用line_shape='hv'for eachgo.Scatter来产生这个:这样,plotly 负责数据的可视化表示,因此在这种情况下无需应用 pandas。完整代码:import pandas as pdimport plotly.graph_objs as god1 = {'Index': [1, 2, 3, 4, 5, 6],     'Time': ["2020-06-01", "2020-06-02", "2020-06-03", "2020-06-04" ,"2020-06-05" ,"2020-06-06"],     'Pred': [100, -200, 300, -400 , -500, 600]    }d2 = {'Index': [1, 2, 3],     'Time': ["2020-06-01", "2020-06-03","2020-06-06"],     'Actual': [90, 280, 650]    }df1 = pd.DataFrame(data=d1)df2 = pd.DataFrame(data=d2)def plot_over_time(df1, df2):    fig = go.Figure()    traces = []    fig.add_trace(dict(        x=df1['Time'], y=df1['Pred'],        mode='lines+markers',        marker=dict(size=10),        name = "Preds", line_shape='hv'))        fig.add_trace(dict(        x=df2['Time'], y=df2['Actual'],        mode='lines+markers',        marker=dict(size=10),        name = "Actuals", line_shape='hv'))    fig.show()plot_over_time(df1, df2)在这里查看更多详细信息和其他选项。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python