简体   繁体   English

plotly 特快时间线:难以超过 1 天的粒度

[英]plotly express timeline: trouble getting finer than 1 day granularity

I'm drawing a Gantt chart using timeline.我正在使用时间轴绘制甘特图。 I want to use add_shape to draw dependencies, but seem to be constrained to day boundaries.我想使用add_shape来绘制依赖项,但似乎受限于日边界。 The examples athttps://plotly.com/python/time-series/#hiding-nonbusiness-hours hints that time deltas of <1day are possible on an axis of type='date' , but my code doesn't work. https://plotly.com/python/time-series/#hiding-nonbusiness-hours中的示例暗示 <1day 的时间增量可能在type='date'的轴上,但我的代码不起作用。

I'm on the verge of resorting to using an int axis and unix timestamps, which looks like I will then have a bunch more questions about how to format that stuff as dates for the ticks.我即将使用int轴和 unix 时间戳,看起来我会有更多关于如何将这些东西格式化为刻度日期的问题。

import datetime

import plotly.express as PX
import pandas

if __name__ == "__main__":
    schedule=[
        (datetime.date(2022,1,10),datetime.date(2022,1,20), 'Task1A'),
        (datetime.date(2022,1,10),datetime.date(2022,1,20), 'Task2A'),
        (datetime.date(2022,1,20),datetime.date(2022,1,30), 'Task1B'),
        (datetime.date(2022,1,20),datetime.date(2022,1,30), 'Task2B')
    ]

    df=pandas.DataFrame(
        [dict(Task=x[2], Start=x[0], Finish=x[1]) for x in schedule],
        index=[x[2] for x in schedule])

    fig = PX.timeline(df, x_start="Start", x_end="Finish", y="Task")
    fig.update_yaxes(autorange="reversed")

    for i in [0,1]:
        task=schedule[i][2]
        to_task=schedule[i+2][2]

        offset= datetime.timedelta(hours=12*i) # an attempt to move coords by less than a whole day

        fig.add_shape( type='line',
                        x0=df.at[task, "Finish"] + offset, y0=task,
                        x1=df.at[task, "Finish"] + offset, y1=to_task,
                        line=dict(color='red', width=1))

    fig.show()

Output Output

在此处输入图像描述

Desired output所需 output

在此处输入图像描述

You can use a combination of pd.to_datetime() with your dates and pd.DateOffset() like this:您可以将pd.to_datetime()与您的日期和pd.DateOffset()结合使用,如下所示:

fig.add_shape(type="line",
    x0=pd.to_datetime('2009-03-05') + pd.DateOffset(hours=42),
    y0=0, x1=pd.to_datetime('2009-03-05') + pd.DateOffset(hours=42), y1=2,
    line=dict(color="red",width=3)
)

Plot Plot

在此处输入图像描述

Complete code:完整代码:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up

fig.add_shape(type="line",
    x0=pd.to_datetime('2009-03-05'),
    y0=0, x1=pd.to_datetime('2009-03-05'), y1=2,
    line=dict(color="RoyalBlue",width=3)
)

fig.add_shape(type="line",
    x0=pd.to_datetime('2009-03-05') + pd.DateOffset(hours=42),
    y0=0, x1=pd.to_datetime('2009-03-05') + pd.DateOffset(hours=42), y1=2,
    line=dict(color="red",width=3)
)

f = fig.full_figure_for_development(warn=False)
fig.show()

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

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