How to plot stacked event duration (Gantt Charts)

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dt

# using df from the OP

# convert columns to a datetime dtype
df.amin = pd.to_datetime(df.amin)
df.amax = pd.to_datetime(df.amax)

fig, ax = plt.subplots(figsize=(8, 5))
ax = ax.xaxis_date()
ax = plt.hlines(df.index, dt.date2num(df.amin), dt.date2num(df.amax))

enter image description here

  • The following code also works
# using df from the OP

df.amin = pd.to_datetime(df.amin)
df.amax = pd.to_datetime(df.amax)

fig, ax = plt.subplots(figsize=(8, 5))
ax = plt.hlines(df.index, df.amin, df.amax)

Leave a Comment