How to make Gantt Charts in Python with Plotly. Gantt Charts use horizontal bars to represent the start and end times of tasks.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
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.
In [1]:
import plotly.express as px import pandas as pd df = pd.DataFrame([ dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'), dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'), dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30') ]) 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()
In [2]:
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.show()
In [3]:
import plotly.express as px import pandas as pd df = pd.DataFrame([ dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Completion_pct=50), dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Completion_pct=25), dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Completion_pct=75) ]) 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([ 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="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'), dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'), dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')] fig = ff.create_gantt(df) fig.show()Group Tasks Together¶
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'), dict(Task="Job-1", Start='2017-02-15', Finish='2017-03-15', Resource='Incomplete'), dict(Task="Job-2", Start='2017-01-17', Finish='2017-02-17', Resource='Not Started'), dict(Task="Job-2", Start='2017-01-17', Finish='2017-02-17', Resource='Complete'), dict(Task="Job-3", Start='2017-03-10', Finish='2017-03-20', Resource='Not Started'), dict(Task="Job-3", Start='2017-04-01', Finish='2017-04-20', Resource='Not Started'), dict(Task="Job-3", Start='2017-05-18', Finish='2017-06-18', Resource='Not Started'), dict(Task="Job-4", Start='2017-01-14', Finish='2017-03-14', 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, group_tasks=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), dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=60), dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Complete=95)] fig = ff.create_gantt(df, colors='Viridis', index_col='Complete', show_colorbar=True) fig.show()What About Dash?¶
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(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4