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

前端 未结 4 846
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 20:50

I have a Pandas DataFrame containing the date that a stream gage started measuring flow and the date that the station was decommissioned. I want to generate a plot showing

4条回答
  •  春和景丽
    2020-11-27 21:37

    I think you are trying to create a gantt plot. This suggests using hlines:

    from datetime import datetime
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.dates as dt
    
    df = pd.read_csv('data.csv')
    df.amin = pd.to_datetime(df.amin).astype(datetime)
    df.amax = pd.to_datetime(df.amax).astype(datetime)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax = ax.xaxis_date()
    ax = plt.hlines(df.index, dt.date2num(df.amin), dt.date2num(df.amax))
    

提交回复
热议问题