Gantt Chart From Dictionary With Lists Of Discrete Non-contiguous Dates As Values
I'm trying to build a Gantt chart in Python (indifferent to package used... perhaps Plotly?) where the X-axis will be discrete dates (e.g., 2020-01-01, 2020-01-02, ...) and the Y-a
Solution 1:
Here is an example using the given data with matplotlib's horizontal bars (barh
). The dictionary is traversed in reverse order of the keys, as matplotlib draws them starting at the bottom.
Matplotlib allows a myriad of tweeking, for every aspect of the plot.
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
names_dict = {'A': ['2020-01-01', '2020-01-02', '2020-01-31'],
'B': ['2020-01-03'],
'C': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04'] }
# x_min = min([datetime.fromisoformat(d) for dates in names_dict.values() for d in dates])
fig, ax = plt.subplots(figsize=(12,4))
for name inreversed(list(names_dict.keys())):
for d in names_dict[name]:
ax.barh(name, width=1.0, left=mdates.date2num(datetime.fromisoformat(d)),
height=1.0, color='crimson', align='center')
for i inrange(len(names_dict)+1):
ax.axhline(i-0.5, color='black')
ax.xaxis_date()
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=5))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))
# ax.grid(True, axis='x') # to show vertical gridlines at each major tick
ax.autoscale(enable=True, axis='y', tight=True)
plt.show()
Post a Comment for "Gantt Chart From Dictionary With Lists Of Discrete Non-contiguous Dates As Values"