【问题标题】:Plotly: How to change the time resolution along the x-axis?Plotly:如何更改沿 x 轴的时间分辨率?
【发布时间】:2020-09-14 08:17:24
【问题描述】:

我想绘制一些时间序列数据,其中数据的历史部分具有每日分辨率,而当天的数据具有分钟分辨率。有没有办法以某种方式“拆分”x 轴,以便历史数据只显示日期,而当前数据也显示时间?

目前看起来像这样,不太可读

【问题讨论】:

    标签: python plotly plotly-dash


    【解决方案1】:

    我认为唯一可行的方法是将两个子图放在一起。但是使用正确的设置应该使子图达到您所描述的几乎 100%。您只需要调整一些细节,例如:

    fig = make_subplots(rows=1, cols=2,
                        horizontal_spacing = 0,
                        shared_yaxes=True,
                        shared_xaxes=True)
    

    完整代码:

    # import pandas as pd
    import pandas as pd
    import numpy as np
    from datetime import datetime, timedelta
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    # custom function to set the first
    # minute dataset to contiunue from
    # the last day in the day dataset
    def next_day(date):
        s = date
        date = datetime.strptime(s, "%Y-%m-%d")
        next_date = date + timedelta(days=1)
        return(datetime.strftime(next_date, "%Y-%m-%d"))
    
    # data
    np.random.seed(10)
    n_days = 5
    n_minutes = (2*24)
    dfd = pd.DataFrame({'time':[t for t in pd.date_range('2020', freq='D', periods=n_days).format()],
                          'y':np.random.uniform(low=-1, high=1, size=n_days).tolist()})
    
    dfm = pd.DataFrame({'time':[t for t in pd.date_range(next_day(dfd['time'].iloc[-1]), freq='min', periods=n_minutes).format()],
                          'y':np.random.uniform(low=-1, high=1, size=n_minutes).tolist()})
    dfm['y'] = dfm['y'].cumsum()
    
    # subplot setup
    fig = make_subplots(rows=1, cols=2,
                        horizontal_spacing = 0,
                        shared_yaxes=True,
                        shared_xaxes=True)
    
    # trace for days
    fig.add_trace(
        go.Scatter(x=dfd['time'], y=dfd['y'], name = 'days'),
        row=1, col=1
    )
    
    # trace for minutes
    fig.add_trace(
        go.Scatter(x=dfm['time'], y=dfm['y'], name = 'minutes'),
        row=1, col=2
    )
    
    # some x-axis aesthetics
    fig.update_layout(xaxis1 = dict(tickangle=0))
    fig.update_layout(xaxis2 = dict(tickangle=90))
    fig.add_shape( dict(type="line",
                        x0=dfd['time'].iloc[-1],
                        y0=dfd['y'].iloc[-1],
                        x1=dfm['time'].iloc[0],
                        y1=dfm['y'].iloc[0],
                        xanchor = 'middle',
                        xref = 'x1',
                        yref = 'y1',
                        line=dict(dash = 'dash',
                                  color="rgba(0,0,255,0.9)",
                                  width=1
                )))
    
    fig.update_xaxes(showgrid=False)
    fig.update_layout(template = 'plotly_dark')
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2021-07-24
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多