# Gantt Charts in Python

How to make Gantt Charts in Python with Plotly. Gantt Charts use horizontal bars to represent the start and end times of tasks.

New to Plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

A Gantt chart is a type of bar chart that illustrates a project schedule. The chart lists the tasks to be performed on the vertical axis, and time intervals on the horizontal axis. The width of the horizontal bars in the graph shows the duration of each activity.

### Gantt Charts and Timelines with plotly.express¶

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. With px.timeline (introduced in version 4.9) each data point is represented as a horizontal bar with a start and end point specified as dates.

The px.timeline function by default sets the X-axis to be of type=date, so it can be configured like any time-series chart.

Plotly Express also supports a general-purpose px.bar function for bar charts.

In [1]:
import plotly.express as px
import pandas as pd

df = pd.DataFrame([
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()


px.timeline supports discrete color as above, or continuous color as follows.

In [2]:
import plotly.express as px
import pandas as pd

df = pd.DataFrame([
])

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

In [3]:
import plotly.express as px
import pandas as pd

df = pd.DataFrame([
])

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


It is also possible to have multiple bars on the same horizontal line, say by resource:

Note: When setting color to the same value as y, autorange should not be set to reverse, so as to list the value of the Y axis in the same order as the legend entries.

In [4]:
import plotly.express as px
import pandas as pd

df = pd.DataFrame([
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource")
fig.show()


#### Deprecated Figure Factory¶

Prior to the introduction of plotly.express.timeline() in version 4.9, the recommended way to make Gantt charts was to use the now-deprecated create_gantt() figure factory, as follows:

In [5]:
import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),

fig = ff.create_gantt(df)
fig.show()


The following example shows how to use the now-deprecated create_gantt() figure factory to color tasks by a numeric variable.

In [6]:
import plotly.figure_factory as ff

df = [dict(Task="Job-1", Start='2017-01-01', Finish='2017-02-02', Resource='Complete'),

colors = {'Not Started': 'rgb(220, 0, 0)',
'Incomplete': (1, 0.9, 0.16),
'Complete': 'rgb(0, 255, 100)'}

fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True,
fig.show()


#### Color by Numeric Variable¶

The following example shows how to use the now-deprecated create_gantt() figure factory to color tasks by a numeric variable.

In [7]:
import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=10),

fig = ff.create_gantt(df, colors='Viridis', index_col='Complete', show_colorbar=True)
fig.show()


#### Reference¶

For more info on ff.create_gantt(), see the full function reference

Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash at https://dash.plot.ly/installation.

Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:

import plotly.graph_objects as go # or plotly.express as px
fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)