代码之家  ›  专栏  ›  技术社区  ›  intrigued_66

如何在Plotly中将Pandas列设置为日期时间x轴

  •  1
  • intrigued_66  · 技术社区  · 10 月前

    我有三个列表,其中包含要通过Plotly显示的数据。第一列表示Epoch时间戳(以毫秒为单位),第二列和第三列是要绘制的序列。

    我正在尝试正确创建pandas数据帧,以传递给Plotly。

    到目前为止,我有这个:

    import pandas as pd
    
    epoch=[1716591253000, 1716591254000, 1716591255000, 1716591256000]
    series_1=[5,6,7,8]
    series_2=[9,10,11,12]
    
    df = pd.DataFrame(data=zip(epoch,series_1,series_2),columns=['Datetime','Series 1','Series 2'])
    
    print(df)
    

    但我不确定如何告诉panda第一列是日期-时间列,并且在传递给Plotly时需要是x轴。

    我发现了以下时间序列示例:

    https://plotly.com/python/time-series/

    但不幸的是,他们正在加载预先创建的数据

    1 回复  |  直到 10 月前
        1
  •  3
  •   e-motta    10 月前

    将列转换为 datetime ,然后将其用作x轴:

    import plotly.express as px
    
    df['Datetime'] = pd.to_datetime(df['Datetime'], unit='ms')
    
    fig = px.line(df, x="Datetime", y=["Series 1", "Series 2"], title="Time Series Data")
    fig.show()
    

    enter image description here

    推荐文章