Python | Basic Gantt chart using Matplotlib
Last Updated :
21 Jun, 2022
Prerequisites : Matplotlib Introduction
In this article, we will be discussing how to plot a Gantt Chart in Python using Matplotlib.
A Gantt chart is a graphical depiction of a project schedule or task schedule (In OS). It's is a type of bar chart that shows the start and finish dates of several elements of a project that include resources or deadline. The first Gantt chart was devised in the mid-1890s by Karol Adamiecki, a Polish engineer who ran a steelworks in southern Poland and had become interested in management ideas and techniques. Some 15 years after Adamiecki, Henry Gantt, an American engineer, and project management consultant, devised his own version of the chart which became famous as "Gantt Charts".
Some Uses of Gantt Charts :Â
Â
- Project Scheduling.
- Task Scheduling on Processors
A sample Gantt Chart for task scheduling is shown below:Â
Â

We will be using broken_barh types of graph available in matplotlib to draw gantt charts.
Below is the code for generating the above gantt chart :Â
Â
Python3
# Importing the matplotlib.pyplot
import matplotlib.pyplot as plt
# Declaring a figure "gnt"
fig, gnt = plt.subplots()
# Setting Y-axis limits
gnt.set_ylim(0, 50)
# Setting X-axis limits
gnt.set_xlim(0, 160)
# Setting labels for x-axis and y-axis
gnt.set_xlabel('seconds since start')
gnt.set_ylabel('Processor')
# Setting ticks on y-axis
gnt.set_yticks([15, 25, 35])
# Labelling tickes of y-axis
gnt.set_yticklabels(['1', '2', '3'])
# Setting graph attribute
gnt.grid(True)
# Declaring a bar in schedule
gnt.broken_barh([(40, 50)], (30, 9), facecolors =('tab:orange'))
# Declaring multiple bars in at same level and same width
gnt.broken_barh([(110, 10), (150, 10)], (10, 9),
facecolors ='tab:blue')
gnt.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
facecolors =('tab:red'))
plt.savefig("gantt1.png")
Lets's understand the different pieces of codes one by one :Â
fig, gnt = plt.subplots()
- Here, we declared a figure "gnt" for plotting the chart.Â
gnt.set_ylim(0, 50)
gnt.set_xlim(0, 160)
- Here, we declared the limits of X-axis and Y-axis of the chart. By default the lower X-axis and Y-axis limit is 0 and higher limits for both axis is 5 unit more the highest X-axis value and Y-axis value.Â
gnt.set_xlabel('seconds since start')
gnt.set_ylabel('Processor')
- Here, we added labels to the axes. By default, there is no labels.Â
gnt.set_yticks([15, 25, 35])
gnt.set_yticklabels(['1', '2', '3'])
- Here, we added ticks in Y-axis. We can also label them. By default the axes is divides uniformly in the limits.Â
gnt.grid(True)
- Here, we set grid() to True to show grids. By default, it is False.Â
gnt.broken_barh([(40, 50)], (30, 9), facecolors=('tab:orange'))
- Here, we added a bar in the chart. In this example, this bar represent the operation going on for time 40 to (40+50)= 90 sec.
The basic arguments :Â
gnt.broken_barh([(start_time, duration)],
(lower_yaxis, height),
facecolors=('tab:colours'))
- By default color is set to Blue.
We can declare multiple bars at the same time :Â
gnt.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
facecolors=('tab:red'))
- We can also add edge color by setting "edgecolor" attribute to any color.Â
plt.savefig("gantt1.png")
- We saved the figure formed in the png file.Â
Reference : Broken Barh Example Matplotlib Documentation
Similar Reads
Bar chart using Plotly in Python
Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
4 min read
Plot a Pie Chart in Python using Matplotlib
A Pie Chart is a circular statistical plot that can display only one series of data. The area of the chart is the total percentage of the given data. Pie charts in Python are widely used in business presentations, reports, and dashboards due to their simplicity and effectiveness in displaying data d
8 min read
Plotting multiple bar charts using Matplotlib in Python
Matplotlib is a powerful visualization library in Python that allows for the creation of various types of plots, including bar charts. When working with multiple bar charts, we can represent data in two main ways, grouped bar charts (multiple bars within one chart) and separate bar charts (multiple
3 min read
Box Plot in Python using Matplotlib
A Box Plot (or Whisker plot) display the summary of a data set, including minimum, first quartile, median, third quartile and maximum. it consists of a box from the first quartile to the third quartile, with a vertical line at the median. the x-axis denotes the data to be plotted while the y-axis sh
3 min read
Matplotlib.artist.Artist.set() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The Artist class contains Abstract base class for objects that render into a FigureCanvas. All visible elements in a figure are subclasses of Artist. matplotlib.artist.Artist.set() method The set() meth
2 min read
Line chart in Matplotlib - Python
Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
Filled area chart using plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization
6 min read
Polar Charts using Plotly in Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Violinplot in Python using axes class of Matplotlib
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
Multiplots in Python using Matplotlib
Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
3 min read