简体   繁体   English

如何消除 plotly.express.timeline 中连续日期和相邻日期之间的差距?

[英]How to remove gaps between consecutive and adjacent dates in plotly.express.timeline?

In my Python script I gather the datetime objects for two time periods via dateutil.parser like so:在我的 Python 脚本中,我通过dateutil.parser收集两个时间段的 datetime 对象, dateutil.parser所示:

from dateutil import parser

start_date_1 = parser.parse("2010-08-01").date()
end_date_1 = parser.parse("2010-11-05").date()
start_date_2 = parser.parse("2010-11-06").date()
end_date_2 = parser.parse("2010-11-12").date()

When trying to visualize these time periods with plotly.express.timeline , there is a gap between consecutive and directly adjacent dates (2010-11-05 and 2010-11-06).当尝试使用plotly.express.timeline可视化这些时间段时,连续和直接相邻的日期(2010-11-05 和 2010-11-06)之间存在差距。 Is there any way to join these two bars without overlapping the dates?有没有办法在不重叠日期的情况下加入这两个酒吧?

df = pd.DataFrame([
        dict(Task="period_1", Start=start_date_1, Finish=end_date_1, Stack="stack_2"),
        dict(Task="period_2", Start=start_date_2, Finish=end_date_2, Stack="stack_2"),
    ])
    
fig = px.timeline(df,
                x_start="Start",
                x_end="Finish",
                y="Stack",
                color="Task",
                hover_name="Task",
                opacity=.7,
                width=1000)

    fig.update_traces(marker_line_width=1.0, opacity=0.95)

    fig.update_layout(
        barmode="overlay",
        xaxis = dict(
            automargin=True,
            dtick="M1",
            tickformat="%Y-%m-%d",
            type="date",
            showgrid=True,
            rangeslider_visible=True),
        
        yaxis = dict(
            automargin=True,
            visible=False,
            autorange="reversed",
            showgrid=True),
        
        legend=dict(
        title=""))

在此处输入图片说明

When giving a daterange to plotly, the second index will be exclusive.当为 plotly 指定日期范围时,第二个索引将是独占的。 (ie You are creating a bar up to 2010-11-05 but not including it. To fix this, add 1 day to each of you endpoints. (即,您正在创建一个截至2010-11-05的条形图,但不包括它。要解决此问题,请为您的每个端点添加 1 天。

start_date_1 = parser.parse("2010-08-01").date()
end_date_1 = parser.parse("2010-11-06").date()
start_date_2 = parser.parse("2010-11-06").date()
end_date_2 = parser.parse("2010-11-13").date()

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

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