Last active
February 22, 2022 02:03
-
-
Save gibsramen/53eec2cffe034fc15a87219d4f36234a to your computer and use it in GitHub Desktop.
Gantt Chart Using Matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib as mpl | |
from matplotlib import pyplot as plt | |
from matplotlib.patches import Patch | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
# Paper 1 | |
paper_1_start = "02/05/2018" | |
paper_1_experiments_complete = "06/03/2018" | |
paper_1_analysis_complete = "09/23/2018" | |
paper_1_submitted = "12/18/2018" | |
paper_1_revisions_started = "03/25/2019" | |
paper_1_resubmitted = "05/02/2019" | |
paper_1_accepted = "06/01/2019" | |
paper_1_dates = pd.to_datetime(pd.Series([ | |
paper_1_start, | |
paper_1_experiments_complete, | |
paper_1_analysis_complete, | |
paper_1_submitted, | |
paper_1_revisions_started, | |
paper_1_resubmitted, | |
paper_1_accepted | |
])) | |
paper_1_dates.index = [ | |
"Wet Lab Experiments", | |
"Data Analysis", | |
"Manuscript Writing", | |
"Initial Submission", | |
"Revisions", | |
"Resubmission", | |
"Acceptance" | |
] | |
# Paper 2 | |
paper_2_start = "10/15/2018" | |
paper_2_experiments_complete = "04/19/2019" | |
paper_2_analysis_complete = "08/01/2019" | |
paper_2_submitted = "09/29/2019" | |
paper_2_revisions_started = "11/07/2019" | |
paper_2_resubmitted = "12/22/2019" | |
paper_2_accepted = "03/10/2020" | |
paper_2_dates = pd.to_datetime(pd.Series([ | |
paper_2_start, | |
paper_2_experiments_complete, | |
paper_2_analysis_complete, | |
paper_2_submitted, | |
paper_2_revisions_started, | |
paper_2_resubmitted, | |
paper_2_accepted | |
])) | |
paper_2_dates.index = [ | |
"Wet Lab Experiments", | |
"Data Analysis", | |
"Manuscript Writing", | |
"Initial Submission", | |
"Revisions", | |
"Resubmission", | |
"Acceptance" | |
] | |
# Paper 3 | |
paper_3_start = "01/07/2020" | |
paper_3_analysis_complete = "04/16/2020" | |
paper_3_submitted = "05/08/2020" | |
paper_3_revisions_started = "08/30/2020" | |
paper_3_resubmission = "10/22/2020" | |
paper_3_accepted = "3/15/2021" | |
paper_3_dates = pd.to_datetime(pd.Series([ | |
paper_3_start, | |
paper_3_analysis_complete, | |
paper_3_submitted, | |
paper_3_revisions_started, | |
paper_3_resubmission, | |
paper_3_accepted | |
])) | |
paper_3_dates.index = [ | |
"Data Analysis", | |
"Manuscript Writing", | |
"Initial Submission", | |
"Revisions", | |
"Resubmission", | |
"Acceptance" | |
] | |
fig, ax = plt.subplots(1, 1, facecolor="white", dpi=300) | |
palette = sns.color_palette("Set2", 3) | |
args = {"edgecolor": "black", "lw": 0.75} | |
paper_dates_list = [ | |
paper_1_dates, | |
paper_2_dates, | |
paper_3_dates, | |
] | |
paper_diff_list = [x.diff() for x in paper_dates_list] | |
paper_labels = [] | |
tick_labels = [] | |
curr_height = 0 | |
paper_zip = zip(paper_dates_list, paper_diff_list, palette) | |
for j, (dates, diffs, color) in enumerate(paper_zip): | |
_start = curr_height | |
for i, (name, val) in enumerate(dates.iteritems()): | |
if i == dates.shape[0] - 1: | |
break | |
x = [(val, diffs[i + 1])] | |
y = (curr_height, 1) | |
rect = ax.broken_barh(x, y, color=color, **args) | |
xmin, xmax = ax.get_xlim() | |
g = ax.text( | |
x=xmin, | |
y=curr_height+0.5, | |
s=name, | |
ha="right", | |
va="center", | |
fontsize="x-small", | |
color=color, | |
weight="bold", | |
) | |
curr_height += 1 | |
tick_labels.append(g) | |
if j == 0: | |
offset_1 = 0.1 | |
offset_2 = 0 | |
elif j == len(paper_dates_list) - 1: | |
offset_2 = 0.1 | |
offset_1 = 0 | |
else: | |
offset_1 = 0 | |
offset_2 = 0 | |
ax.axhspan( | |
_start - offset_1, | |
curr_height + offset_2, | |
color=color, | |
zorder=0, | |
alpha=0.3 | |
) | |
xmin, xmax = ax.get_xlim() | |
for label in tick_labels: | |
label.set_x(xmin - 10) | |
ax.set_yticks(np.arange(curr_height+1)) | |
ax.set_yticklabels([]) | |
ax.set_ylim([0-0.1, curr_height+0.1]) | |
ax.tick_params("both", labelsize=6) | |
ax.grid(which="both", alpha=0.5) | |
ax.set_axisbelow(True) | |
yr_line_args = {"color": "gray", "lw": 0.7, "zorder": 0} | |
ax.axvline(x=pd.to_datetime("2018-01-01"), **yr_line_args) | |
ax.axvline(x=pd.to_datetime("2019-01-01"), **yr_line_args) | |
ax.axvline(x=pd.to_datetime("2020-01-01"), **yr_line_args) | |
ax.axvline(x=pd.to_datetime("2021-01-01"), **yr_line_args) | |
patch_labels = [f"Paper {x+1}" for x in range(3)] | |
patches = [ | |
Patch(facecolor=palette[i], label=patch_labels[i], edgecolor="black") | |
for i in range(3) | |
] | |
ax.legend( | |
handles=patches, | |
framealpha=0, | |
ncol=3, | |
loc="lower center", | |
bbox_to_anchor=[0.5, 1], | |
fontsize="small" | |
) | |
ax.yaxis.set_tick_params(width=0) | |
plt.savefig("gantt.png", bbox_inches="tight", dpi=300) |
Author
gibsramen
commented
Feb 22, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment