The Wayback Machine - https://web.archive.org/web/20240810101922/https://www.geeksforgeeks.org/python-basic-gantt-chart-using-matplotlib/
Open In App

Python | Basic Gantt chart using Matplotlib

Last Updated : 21 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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



Previous Article
Next Article

Similar Reads

Gantt Chart in plotly
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 library. Gantt Chart A Gantt chart is parallel to bar cha
2 min read
Donut Chart using Matplotlib in Python
Prerequisites: Pie Chart in matplotlib Donut charts are the modified version of Pie Charts with the area of center cut out. A donut is more concerned about the use of area of arcs to represent the information in the most effective manner instead of Pie chart which is more focused on comparing the proportion area between the slices. Donut charts are
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 are commonly used in business presentations like sales, operations, survey results, resources, etc. as they provide a quick summary. In this article, let's understand how to create pie char
5 min read
Calculate Spider Chart Values Based on Bounding Boxes Inside Chart Segments
The Spider charts also known as radar or spider web charts are useful for visualizing multivariate data in the form of a two-dimensional chart with the multiple axes originating from the same point. Calculating values for the spider chart segments can be challenging especially when dealing with irregular shapes such as the bounding boxes. In this a
3 min read
How to display the value of each bar in a bar chart using Matplotlib?
In this article, we are going to see how to display the value of each bar in a bar chart using Matplotlib. There are two different ways to display the values of each bar in a bar chart in matplotlib - Using matplotlib.axes.Axes.text() function.Use matplotlib.pyplot.text() function. Example 1: Using matplotlib.axes.Axes.text() function: This functio
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 line charts and matplotlib simple line plots in Pytho
4 min read
How to set border for wedges in Matplotlib pie chart?
Pie charts can be used for relative comparison of data. Python offers several data visualization libraries to work with. The Matplotlib library offers different types of graphs and inbuild methods and properties to manipulate the graph. The wedges in the pie chart can be given a border using the wedgeprops attribute of the pie() method of matplotli
3 min read
Radially displace pie chart wedge in Matplotlib
Pie charts are statistical graphs divided into slices that represent different data values and sum up to 100%. Python is one of the most popularly used programming languages for data visualization. Python has multiple data visualization libraries and Matplotlib is one of them. Matplotlib is widely used because of its simplicity and ease of implemen
3 min read
Draw a horizontal bar chart with Matplotlib
Matplotlib is the standard python library for creating visualizations in Python. Pyplot is a module of Matplotlib library which is used to plot graphs and charts and also make changes in them. In this article, we are going to see how to draw a horizontal bar chart with Matplotlib. Creating a vertical bar chart Approach: Importing matplotlib.pyplot
2 min read
Plot Multiple Columns of Pandas Dataframe on Bar Chart with Matplotlib
Prerequisites: PandasMatplotlib In this article, we will learn how to plot multiple columns on bar chart using Matplotlib. Bar Plot is used to represent categories of data using rectangular bars. We can plot these bars with overlapping edges or on same axes. Different ways of plotting bar graph in the same chart are using matplotlib and pandas are
2 min read
three90RightbarBannerImg