0% found this document useful (0 votes)
7 views

Gannt Chart Python Code

The document provides Python code for creating a Gantt chart using Matplotlib, detailing tasks, milestones, and dependencies for a project. It includes configurations for visual style, task representation, and milestone markers, as well as adjustments for the x-axis and legend. The final output is a Gantt chart visualizing the project timeline and phases.

Uploaded by

Ras Rafiki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Gannt Chart Python Code

The document provides Python code for creating a Gantt chart using Matplotlib, detailing tasks, milestones, and dependencies for a project. It includes configurations for visual style, task representation, and milestone markers, as well as adjustments for the x-axis and legend. The final output is a Gantt chart visualizing the project timeline and phases.

Uploaded by

Ras Rafiki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Below is the python code for the gantt chart:

import matplotlib.pyplot as plt import matplotlib.dates as mdates from


matplotlib.patches import Patch import datetime

Use "ggplot" style for improved appearance.

plt.style.use('ggplot')

---------------------------

Define Tasks (Key Activities)

---------------------------

tasks = [ { "Task": "Market Research", "Start": "Feb 17, 2025", "End": "Mar 2,
2025", "Color": "#70AD47" # Planning Phase }, { "Task": "Two Domestic
Safari Package Development", "Start": "Mar 3, 2025", "End": "Mar 16, 2025",
"Color": "#70AD47" # Planning Phase }, { "Task": "Social Media Campaign",
"Start": "Mar 17, 2025", "End": "Apr 6, 2025", "Color": "#ED7D31" #
Implementation Phase }, { "Task": "Vehicle Optimization", "Start": "Mar 17,
2025", "End": "Apr 20, 2025", "Color": "#ED7D31" # Implementation
Phase }, { "Task": "Local Trip Highlights", "Start": "Mar 17, 2025", "End":
"Apr 6, 2025", "Color": "#ED7D31" # Implementation Phase }, { "Task":
"Repeat-Customer Discount Program", "Start": "Apr 7, 2025", "End": "May 11,
2025", "Color": "#ED7D31" # Implementation Phase }, { "Task": "Progress
Monitoring", "Start": "Mar 3, 2025", "End": "May 11, 2025", "Color":
"#FFC000" # Monitoring } ]

---------------------------

Define Milestones

---------------------------

milestones = [ {"label": "Market Research Completed", "date": "Mar 2,


2025"}, {"label": "Two Domestic Safari Packages Designed", "date": "Mar 16,
2025"}, {"label": "Social Media Content Ready", "date": "Mar 30, 2025"},
{"label": "Manager Trained on App", "date": "Mar 30, 2025"}, {"label":
"Campaign Gains 150 Followers", "date": "Apr 6, 2025"}, {"label": "5
Highlights Posted", "date": "Apr 6, 2025"}, {"label": "20% Fuel Savings
Achieved", "date": "Apr 20, 2025"}, {"label": "10+ Bookings Secured",
"date": "Apr 27, 2025"}, {"label": "Discount Program Live", "date": "May 11,
2025"}, {"label": "Final Report Submitted", "date": "May 11, 2025"} ]
---------------------------

Define Dependencies (by task index)

---------------------------

dependencies = [ {"from": 0, "to": 1}, {"from": 1, "to": 2}, {"from": 1, "to":


3}, {"from": 1, "to": 4}, {"from": 2, "to": 5} ]

Date format for parsing

date_format = "%b %d, %Y"

Parse dates for tasks

for task in tasks: task["Start_dt"] = datetime.datetime.strptime(task["Start"],


date_format) task["End_dt"] = datetime.datetime.strptime(task["End"],
date_format) task["Duration"] = (task["End_dt"] - task["Start_dt"]).days

Parse milestone dates

for ms in milestones: ms["Date_dt"] =


datetime.datetime.strptime(ms["date"], date_format)

---------------------------

Create the Figure and Axes

---------------------------

fig, ax = plt.subplots(figsize=(12, 8))

Set y positions for tasks (reserve extra space at the top for milestones)

num_tasks = len(tasks) y_positions = range(num_tasks)

Plot each task as a horizontal bar.

for i, task in enumerate(tasks): ax.barh(i, task["Duration"],


left=task["Start_dt"], height=0.5, color=task["Color"], edgecolor="black",
alpha=0.9) # Place the task name inside the bar. mid_date =
task["Start_dt"] + datetime.timedelta(days=task["Duration"] / 2)
ax.text(mid_date, i, task["Task"], va='center', ha='center', color="white",
fontsize=9, fontweight='bold')

---------------------------

Draw Dependency Arrows

---------------------------
for dep in dependencies: source = tasks[dep["from"]] target =
tasks[dep["to"]] start_x = source["End_dt"] end_x = target["Start_dt"] start_y
= dep["from"] end_y = dep["to"] # Draw an arrow from the end of the
source task to the beginning of the target task. ax.annotate("", xy=(end_x,
end_y + 0.25), xytext=(start_x, start_y - 0.25),
arrowprops=dict(arrowstyle="->", color="gray", lw=1.5,
connectionstyle="arc3,rad=-0.3"))

---------------------------

Plot Milestone Markers

---------------------------

Fixed y-coordinate above tasks for milestones.

milestone_y = num_tasks + 0.5 for ms in milestones:


ax.scatter(ms["Date_dt"], milestone_y, marker="D", s=100,
color="#4472C4", edgecolor="black", zorder=5) ax.text(ms["Date_dt"],
milestone_y + 0.3, ms["label"], rotation=45, fontsize=8, ha='center',
va='bottom')

---------------------------

Configure the X-Axis with Dates

---------------------------

ax.xaxis_date()
ax.xaxis.set_major_locator(mdates.WeekdayLocator(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))
plt.xticks(rotation=45) plt.xlabel("Date") plt.ylabel("Tasks")
plt.title("Capstone Project Gantt Chart", fontsize=16, fontweight='bold')

Expand y-limits to show milestones.

ax.set_ylim(-0.75, num_tasks + 1)

---------------------------

Add a Legend

---------------------------

legend_elements = [ Patch(facecolor="#70AD47", edgecolor="black",


label="Planning Phase"), Patch(facecolor="#ED7D31", edgecolor="black",
label="Implementation Phase"), Patch(facecolor="#FFC000",
edgecolor="black", label="Monitoring"), Patch(facecolor="#4472C4",
edgecolor="black", label="Milestones") ]
ax.legend(handles=legend_elements, loc="lower right")

---------------------------

Final Adjustments and Show Plot

---------------------------

plt.tight_layout() plt.show()

-----------------------------------------------

Boxed Final Answer:

{Gantt Chart and Quality Management Plan updated as per client's


instructions.}

-----------------------------------------------

The following is the snippet for the code above:


Running the code above in a code editor leads to the gannt chart below:

You might also like