Gantt Chart in Python
A Gantt chart is a graphical representation of a project schedule, this is an easiest way to evaluate the tasks and projects. In this the project schedules are shown by using a bar chart with start and finish dates, so we can easily evaluate the project schedule. In gantt chart the tasks to be performed is denoted on the vertical axis and time interval on the horizontal axis.
In python the gantt chart is implemented by using some libraries such as gantt, rsvg, cairo etc, are used for the creation of simple gantt chart, if we want generate a complicated one then use other related libraries.
We can create gantt chart in 2 ways
1) Using gantt
2) Using Plotly
Using gantt library
Installing python-gantt is just as simple as :
pip install python-gantt
Example for creating gantt chart by using gantt library as mentioned below.
import datetime
import gantt
import cairo
# Change font default
gantt.define_font_attributes(fill='black',
stroke='black',
stroke_width=0,
font_family="Verdana")
# Create some tasks
t1 = gantt.Task(name='Task 1',
start=datetime.date(2017, 12, 1),
duration=4,
percent_done=44,
color="#FF8080")
t2 = gantt.Task(name='Task 2',
start=datetime.date(2017, 12, 28),
duration=6,
)
# Create a project
p1 = gantt.Project(name='Project 1')
# Add tasks to this project
p1.add_task(t1)
p1.add_task(t2)
##########################$ MAKE DRAW ###############
t = p1.make_svg_for_tasks(filename='gantt_chart.svg',
start=datetime.date(2017, 11, 27),
end=datetime.date(2018, 01, 06),
scale=gantt.DRAW_WITH_DAILY_SCALE)
handle= rsvg.Handle("/home/zbeanz/Desktop/gantt_chart.svg")
img = cairo.ImageSurface(cairo.FORMAT_ARGB32, handle.get_dimension_data()[0],handle.get_dimension_data()[1], world_readable=True)
ctx = cairo.Context(img)
handle.render_cairo(ctx)
The output of this program gives a svg file that contain project 'Project 1’ and tasks t1 and t2, as shown in the figure.
The above program include one project with two tasks, we add more than one projects with tasks.
Using Plotly library
1) Plotly's Python library is free and open source.
2) We can set up Plotly to work in online or offline mode. It is mainly used for map plotting.
3) Installing plotly is just as simple as :
pip install plotly
--upgrade to update your Plotly version
4) This is an example for creating gantt chart by using gantt library
import sys
sys.path.insert(0, 'c:\pyzo2015a\lib\site-packages\plotly')
import plotly as py
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)
py.offline.plot(fig, filename='gantt-numeric-variable‘)
The output of this program is shown below
This is an another example for creating gantt chart by using gantt library with numeric variable as index.
import sys
sys.path.insert(0, 'c:\pyzo2015a\lib\site-packages\plotly')
import plotly as py
import plotly.figure_factory as ff
df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=80),
dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=75),
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)
py.offline.plot(fig, filename='gantt-numeric-variable')
The output of the above program is shown below
This is an another example for creating gantt chart by using gantt library with string variable as index
import sys
sys.path.insert(0, 'c:\pyzo2015a\lib\site-packages\plotly')
import plotly as py
import plotly.figure_factory as ff
df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource='Apple'),
dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Resource='Grape'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource='Banana')]
colors = ['#7a0504', (0.2, 0.7, 0.3), 'rgb(210, 60, 180)']
fig = ff.create_gantt(df, colors=colors, index_col='Resource', reverse_colors=True,show_colorbar=True)
The output of this program is shown below.
Advantages of Gantt Charts
1) It creates a picture of complexity : - If we can see complex ideas as a picture, this will help our understanding.
2) It demonstrates that you know what you’re doing : - It speaks volumes about whether you are on top of the needs of the project and whether the project will be successful.
3) It (should) help you to set realistic time frames : - The bars on the chart indicate in which period a particular task or set of tasks will be completed.
4) It can be highly visible : - It can be useful to place the chart, or a large version of it, where everyone can see it.
Disadvantages of Gantt Charts
1) They can become extraordinarily complex : - Large number of tasks will create a complex chart. The below figure is an example for this issue
2) The size of the bar does not indicate the amount of work : - Each bar on the chart indicates the time period over which a particular set of tasks will be completed.
3) Difficult to see on one sheet of paper.
From this we get an idea about Gantt chart and how it implemented in python. I hope that this will be helpful to manage Gantt chart in python.
Thank you.