Skip to content

Instantly share code, notes, and snippets.

@aabiddanda
Last active June 27, 2023 01:33
Show Gist options
  • Save aabiddanda/2a5eaa671e26898c3b398d32473a2430 to your computer and use it in GitHub Desktop.
Save aabiddanda/2a5eaa671e26898c3b398d32473a2430 to your computer and use it in GitHub Desktop.
"""
GANTT Chart with Matplotlib
Sukhbinder
Inspired from
https://sukhbinder.wordpress.com/2016/05/10/quick-gantt-chart-with-matplotlib/
"""
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.dates
from matplotlib.dates import WEEKLY,MONTHLY, DateFormatter, rrulewrapper, RRuleLocator
import numpy as np
import click
def _create_date(datetxt):
"""Creates the date"""
day,month,year=datetxt.split('-')
date = dt.datetime(int(year), int(month), int(day))
mdate = matplotlib.dates.date2num(date)
return mdate
def CreateGanttChart(fname, outname):
"""
Create gantt charts with matplotlib
Input is a data file for the GANNT chart
Output is a image file for the figure
"""
ylabels = []
customDates = []
try:
textlist=open(fname).readlines()
except:
return
for tx in textlist:
if not tx.startswith('#'):
ylabel,startdate,enddate=tx.split(',')
ylabels.append(ylabel.replace('\n',''))
customDates.append([_create_date(startdate.replace('\n','')),_create_date(enddate.replace('\n',''))])
ilen=len(ylabels)
pos = np.arange(0.5,ilen*0.5+0.5,0.5)
task_dates = {}
for i,task in enumerate(ylabels):
task_dates[task] = customDates[i]
fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
for i in range(len(ylabels)):
start_date,end_date = task_dates[ylabels[i]]
ax.barh((i*0.5)+0.5, end_date - start_date, left=start_date, height=0.3, align='center', color='blue', alpha = 0.8)
locsy, labelsy = plt.yticks(pos,ylabels)
plt.setp(labelsy, fontsize = 14)
# ax.axis('tight')
ax.set_ylim(ymin = -0.1, ymax = ilen*0.5+0.5)
ax.grid(color = 'g', linestyle = ':')
ax.xaxis_date()
rule = rrulewrapper(WEEKLY, interval=1)
loc = RRuleLocator(rule)
formatter = DateFormatter("%d-%b")
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(formatter)
labelsx = ax.get_xticklabels()
plt.setp(labelsx, rotation=30, fontsize=10)
font = font_manager.FontProperties(size='small')
ax.legend(loc=1,prop=font)
ax.invert_yaxis()
fig.autofmt_xdate()
plt.savefig(outname)
@click.command()
@click.option('--gannt_file', required=True, help='File with dates for the GANNT file creation')
@click.option('--out', required=True, default='gannt.png', help='Output Figure with ')
def main(gannt_file, out):
CreateGanttChart(gannt_file, out)
if __name__ == '__main__':
main()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Requirements/Design 04-01-2016 26-02-2016
Development 20-02-2016 27-03-2016
Test Planning 27-03-2016 3-04-2016
Testing 30-03-2016 30-04-2016
Bug Fixes 08-04-2016 15-05-2016
Documentation 15-04-2016 08-05-2016
Deployment 15-05-2016 25-05-2016
Final Report 8-05-2016 25-06-2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment