Gantt chart in plotly

Sample data

A Gantt chart represents a set of tasks and their estimated execution time by using horizontal rectangles. In the following sample data there are five tasks, the start and end date, the person assigned to do it and the level of difficulty of the task.

import pandas as pd

df = pd.DataFrame([
    dict(Task = "Task A", Start = '2013-09-05', End = '2013-10-25', Assigned = "Person A", Difficulty = 70),
    dict(Task = "Task B", Start = '2013-12-03', End = '2014-02-14', Assigned = "Person A", Difficulty = 20),
    dict(Task = "Task C", Start = '2013-10-20', End = '2014-03-17', Assigned = "Person B", Difficulty = 30),
    dict(Task = "Task D", Start = '2014-02-21', End = '2014-07-06', Assigned = "Person A", Difficulty = 50),
    dict(Task = "Task E", Start = '2014-06-20', End = '2014-09-28', Assigned = "Person B", Difficulty = 80)
])

Gantt charts with timeline

Plotly Python provides a function named timeline from the plotly express module to create Gantt charts easily. You will need to input your data frame and specify the names of the columns containing the start and end dates to x_start, x_end and the column that contains the tasks to y. Set autorange = "reversed" to order the tasks from top to bottom.

Basic Gantt chart in Plotly

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Adding a title

The title argument can be used to set a title to the figure.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task", title = "Tasks")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Y-axis label

You can also override the default Y-axis label passing a dict to labels, being the key the name of the column passed to y. If you want to set a title for the X-axis yo will need to use the xaxis_title argument from update_layout.

import plotly.express as px
import pandas as pd

# Custom Y-axis label
y_label = {'Task': 'Y-axis label'}

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task", labels = y_label)
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed")
# X-axis label
fig.update_layout(xaxis_title = "X-axis label") 
fig.show()

Gantt with texts

The timeline function provides an argument named text where you can input an array with texts or specify the name of the column to be used to create the texts.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task", text = "Difficulty")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Color customization

There are several ways to customize the color of a time line or Gantt chart made with plotly express.

Color of the bars

If you want to override the default color of the bars you will need to specify a new color making use of the color_discrete_sequence argument. Note that the color must be specified using an array.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task",
                  color_discrete_sequence = ["darkorange"])
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Different color for each task

In case you want to set a different color for each task you will need to create a dict specifying the desired color for each task and pass it as input to color_discrete_map. In this scenario you won’t need to reverse the scale of the Y axis.

import plotly.express as px
import pandas as pd

colors = {"Task A": "red", "Task B": "orange", "Task C": "blue",
          "Task D": "purple", "Task E": "yellow"}

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task",
                  color = "Task", color_discrete_map = colors)
fig.show()

Color based on the person assigned (discrete variable)

The color argument of the timeline function can be used to set the color of the tasks based on a categorical variable. In the example below we are using a column named "Assigned" which specifies the person the task is assigned to.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task",
                  color = "Assigned")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Color selection

It is possible to customize the colors if your bars are colored based on a categorical variable passing an array with as many colors as groups to the color_discrete_sequence argument of the function.

import plotly.express as px
import pandas as pd

colors = ["darkorange", "dodgerblue"]

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task", color = "Assigned",
                  color_discrete_sequence = colors)
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Color based on the difficulty (continuous variable)

The color can also be based on a numerical variable, representing, for instance, the difficulty or the level of completion of the task.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task",
                  color = "Difficulty")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()

Continuous color scale

The midpoint of the continuous scale can be customized with color_continuous_midpoint. In addition, if you want to override the default color scale you can specify a new continuous palette with color_continuous_scale.

import plotly.express as px
import pandas as pd

fig = px.timeline(df, x_start = "Start", x_end = "End", y = "Task",
                  color = "Difficulty", color_continuous_scale = "viridis")
# Tasks from top to bottom
fig.update_yaxes(autorange = "reversed") 
fig.show()
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also