Add custom markers to Gantt Chart in Plotly

Viewed 585

I’m new to plotly and I’m creating a gantt chart using px.timeline. There are 3 categories of data in my dataset, a normal task with a start and end time, and two types of task where the start and end time are same. I want the normal task to be a rectangle (which is how it is being plot) and the other two tasks to have a hourglass marker and a triangle marker instead of a very thin line ?

This is how my data looks :

data = [dict(Task=’’, start=’’, end=’’, shape=’<rect, hour, tri>’)]

Sample Data :

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-01-01', shape='hourglass'),
      dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', shape='rectangle'),
      dict(Task="Job C", Start='2009-05-30', Finish='2009-05-30', shape='triangle')]

Code :

fig = px.timeline(data, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed", ticklabelposition="outside left")
fig.update_layout(showlegend=False, height=2000, width=1255, margin_pad=10)
fig.show()

Example:

Sample Plot in Excel

Is there any way I can achieve this ?

Thanks !

1 Answers

I solved this after some hours of searching.

Split the data into three each corresponding to 3 different shapes and then plot and combine.

Create 3 individual Plots :

rect = px.timeline(rect, x_start="Start", x_end="Finish", y="Task", color="color")
dia = px.scatter(dia, x="Start", y="Task", color="color", symbol_sequence=['diamond'])
coll = px.scatter(coll, x="Start", y="Task", color="color", symbol_sequence=['hourglass'])

Update traces for individual plots if needed :

rect.update_traces(marker=dict(line=dict(width=1, color='black')))
dia.update_traces(marker=dict(size=12, line=dict(width=2)))
coll.update_traces(marker=dict(size=12, line=dict(width=2)))

Set the timeline plot's axis:

rect.update_xaxes(tickformat="%H:%M:%S.%L", tickmode='linear', dtick='120000')
rect.update_yaxes(autorange='reversed')
rect.update_layout(title=title, showlegend=False, height=2800, width=2000)

Overlay all Plots:

new_fig = go.Figure(data=rect.data + dia.data + coll.data, layout=rect.layout)
new_fig.show()
Related