How to plot stacked event duration (Gantt Charts) using Python Pandas How to plot stacked event duration (Gantt Charts) using Python Pandas python python

How to plot stacked event duration (Gantt Charts) using Python Pandas


  • I think you are trying to create a gantt plot.
  • This suggests using hlines
  • Tested in matplotlib 3.4.2
import pandas as pdimport matplotlib.pyplot as pltimport matplotlib.dates as dt# using df from the OP# convert columns to a datetime dtypedf.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 OPdf.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)


You can use Bokeh (a python library) to make gantt chart and its really beautiful.Here is a code I copied from a twiiter.http://nbviewer.jupyter.org/gist/quebbs/10416d9fb954020688f2

from bokeh.plotting import figure, show, output_notebook, output_filefrom bokeh.models import ColumnDataSource, Range1dfrom bokeh.models.tools import HoverToolfrom datetime import datetimefrom bokeh.charts import Baroutput_notebook()#output_file('GanntChart.html') #use this to create a standalone html file to send to othersimport pandas as psDF=ps.DataFrame(columns=['Item','Start','End','Color'])Items=[    ['Contract Review & Award','2015-7-22','2015-8-7','red'],    ['Submit SOW','2015-8-10','2015-8-14','gray'],    ['Initial Field Study','2015-8-17','2015-8-21','gray'],    ['Topographic Procesing','2015-9-1','2016-6-1','gray'],    ['Init. Hydrodynamic Modeling','2016-1-2','2016-3-15','gray'],    ['Prepare Suitability Curves','2016-2-1','2016-3-1','gray'],    ['Improvement Conceptual Designs','2016-5-1','2016-6-1','gray'],    ['Retrieve Water Level Data','2016-8-15','2016-9-15','gray'],    ['Finalize Hydrodynamic Models','2016-9-15','2016-10-15','gray'],    ['Determine Passability','2016-9-15','2016-10-1','gray'],    ['Finalize Improvement Concepts','2016-10-1','2016-10-31','gray'],    ['Stakeholder Meeting','2016-10-20','2016-10-21','blue'],    ['Completion of Project','2016-11-1','2016-11-30','red']    ] #first items on bottomfor i,Dat in enumerate(Items[::-1]):    DF.loc[i]=Dat#convert strings to datetime fields:DF['Start_dt']=ps.to_datetime(DF.Start)DF['End_dt']=ps.to_datetime(DF.End)G=figure(title='Project Schedule',x_axis_type='datetime',width=800,height=400,y_range=DF.Item.tolist(),        x_range=Range1d(DF.Start_dt.min(),DF.End_dt.max()), tools='save')hover=HoverTool(tooltips="Task: @Item<br>\Start: @Start<br>\End: @End")G.add_tools(hover)DF['ID']=DF.index+0.8DF['ID1']=DF.index+1.2CDS=ColumnDataSource(DF)G.quad(left='Start_dt', right='End_dt', bottom='ID', top='ID1',source=CDS,color="Color")#G.rect(,"Item",source=CDS)show(G)