pythonplotlygantt-chartplotly-express

Tasks with the same name in Gantt diagram using plotly.express.timeline


Is there a way to separate tasks on a Gantt diagram if they have the same names?

import plotly.express as px
import pandas as pd

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

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Worker")
fig.update_yaxes(autorange="reversed") 
fig.show()

I want to show two jobs named "Job A" as separate tasks, not grouping and not in one line using opacity, and preferably not using deprecated plotly.figure_factory.create_gantt. Thanks in advance!


Solution

  • You could do something like this :

    df = pd.DataFrame([
        dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Worker='Alex'),
        dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Worker='John'),
        dict(Task="Job A", Start='2009-02-20', Finish='2009-05-30', Worker='Marc')
    ])
    
    # Add a column to help identify unique task per worker
    df['id'] = df['Task'] + ':' + df['Worker']
    df = df.sort_values('id').reset_index()
    
    # Use that column instead
    fig = px.timeline(df, x_start="Start", x_end="Finish", y='id', color="Worker")
    
    # Optionally, if you want to preserve original yaxis title and tick labels
    fig.update_yaxes(title='Task', tickmode='array', tickvals=df['id'], ticktext=df['Task'])
    
    fig.update_yaxes(autorange="reversed")
    fig.show()