为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

幕布斯7119047 2023-01-04 16:28:58
我有两个熊猫系列。一个有所有日期的条目,一个有零星的条目。在下面的示例中绘制df2['Actual']时。在每个时间点绘制最新值而不是在每个记录点之间画一条线的最佳方法是什么。在此示例中,该Actuals线将绘制在 y 轴上的 90 处,直到 2020 年 6 月 3 日将跳至 280。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"))        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)
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超7个赞

使用line_shape='hv'for eachgo.Scatter来产生这个:

//img1.sycdn.imooc.com//63b538f10001676506580286.jpg

这样,plotly 负责数据的可视化表示,因此在这种情况下无需应用 pandas。


完整代码:


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", 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)

这里查看更多详细信息和其他选项。



查看完整回答
反对 回复 2023-01-04
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信