✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

How to plot durations (start time-finish time) as a horizontal lines

I have a series of start and finish times, from which I want to plot horizontal duration lines.

( (start1, finish1), (start2, finish2) … ) where start and finish are timestamps

I cannot find any guidance on how to achieve this either in the documentation or by other searching.

One possible workaround might be to make a series of narrow rectangles, but that feels like a kludge, when others enthuse about the wonders of Plotly !!

Hi @mike0whit,
Welcome to forum! Plotly provides the Gant chart, that with right parameters draws horizontal line from start to end time of a job. There exist two versions: ff.create_gantt(), and px.timeline() to define such a chart:

import plotly.figure_factory as ff
from datetime import datetime

df = [dict(Task="Job A", Start='2019-01-01', Finish='2019-02-01', Resource='Apple'),
      dict(Task="Job B", Start='2019-03-05', Finish='2019-04-15', Resource='Grape'),
      dict(Task="Job C", Start='2019-04-20', Finish='2019-09-30', Resource='Banana')]

colors = ['#7a0504', (0.2, 0.7, 0.3), 'rgb(210, 60, 180)']

fig = ff.create_gantt(df, colors=colors, index_col='Resource', 
                      showgrid_x=True, showgrid_y=False,
                      show_colorbar=True, bar_width=0.025)
fig.update_layout(height=400, width=800)
fig.show()

for more details print:
help(ff.create_gantt)

or

import plotly.express as px
import pandas as pd

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

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
fig.update_yaxes(autorange="reversed")
fig.update_traces(width=0.025)  #This update generates a thinner bar (like a line) instead of  the default horizontal bar
fig.show()

To get help on px.timeline()
print help(px.timeline)

Thanks very much ! I am using that now.

However, my start and end times are in UTC, ISO format, but the graph has a weird X axis (1000AD - 6000AD) rather than the last two days !!
With a flurry around August 2020 !

data sample:

{‘instance’: ‘i-123’, ‘start’: ‘2020-08-15T17:28:19+00:00’, ‘end’: ‘2020-08-15T17:33:01+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T15:43:56+00:00’, ‘end’: ‘2020-08-14T15:43:58+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T16:40:26+00:00’, ‘end’: ‘2020-08-14T17:34:42+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T15:29:49+00:00’, ‘end’: ‘2020-08-14T15:29:50+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T16:38:57+00:00’, ‘end’: ‘2020-08-14T16:40:24+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T15:30:00+00:00’, ‘end’: ‘2020-08-14T15:30:01+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T15:29:56+00:00’, ‘end’: ‘2020-08-14T15:29:57+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T17:34:46+00:00’, ‘end’: ‘2020-08-14T18:42:31+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T15:29:58+00:00’, ‘end’: ‘2020-08-14T15:29:59+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-17T21:10:55+00:00’, ‘end’: ‘2020-08-17T21:13:28+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-14T14:55:53+00:00’, ‘end’: ‘2020-08-14T15:50:45+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-17T19:52:10+00:00’, ‘end’: ‘2020-08-17T19:54:42+00:00’}
{‘instance’: ‘i-123’, ‘start’: ‘2020-08-17T19:15:34+00:00’, ‘end’: ‘2020-08-17T19:18:06+00:00’}

@mike0whit

I converted df[‘start’] and df[‘end’] , via pd.to_datetime() and here is what I got:

import plotly.express as px
import pandas as pd
d = [{'instance': 'i-123', 'start': '2020-08-15T17:28:19+00:00', 'end': '2020-08-15T17:33:01+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T15:43:56+00:00', 'end': '2020-08-14T15:43:58+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T16:40:26+00:00', 'end': '2020-08-14T17:34:42+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T15:29:49+00:00', 'end': '2020-08-14T15:29:50+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T16:38:57+00:00', 'end': '2020-08-14T16:40:24+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T15:30:00+00:00', 'end': '2020-08-14T15:30:01+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T15:29:56+00:00', 'end': '2020-08-14T15:29:57+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T17:34:46+00:00', 'end': '2020-08-14T18:42:31+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T15:29:58+00:00', 'end': '2020-08-14T15:29:59+00:00'},
{'instance': 'i-123', 'start': '2020-08-17T21:10:55+00:00', 'end': '2020-08-17T21:13:28+00:00'},
{'instance': 'i-123', 'start': '2020-08-14T14:55:53+00:00', 'end': '2020-08-14T15:50:45+00:00'},
{'instance': 'i-123', 'start': '2020-08-17T19:52:10+00:00', 'end': '2020-08-17T19:54:42+00:00'},
{'instance': 'i-123', 'start': '2020-08-17T19:15:34+00:00', 'end': '2020-08-17T19:18:06+00:00'}]

df = pd.DataFrame(d)
for col in ['start', 'end']:
    df[col] = pd.to_datetime(df[col])

df['id'] = [idx for idx in df.index]

print(df['start'].min(), df['end'].max())

fig = px.timeline(df, x_start="start", x_end="end", y="id") 

fig.update_xaxes(range=[df['start'].min(), df['end'].max()])  #THIS IS A NECESSARY UPDATE!!!!
fig.update_traces(width=0.15)
fig.show()

Data from the last two rows of the dataframe are not displayed. This is the time difference:

df['end'] -  df['start'] 

0    00:04:42
1    00:00:02
2    00:54:16
3    00:00:01
4    00:01:27
5    00:00:01
6    00:00:01
7    01:07:45
8    00:00:01
9    00:02:33
10   00:54:52
11   00:02:32
12   00:02:32
dtype: timedelta64[ns]