plot patch rectangle of range from different rows in pandas

Let’s try plotting one bar after another:

plt.figure(figsize=(10,3))
for i,r in df.iterrows():
    plt.barh(r['date'], width=r['end']-r['start'],
             left=r['start'], color='y' if r['cat']=='cat1' else 'g')

Or plot all of them together with mapping the color:

plt.barh(df['date'], width=df['end']-df['start'],
         left=df['start'], color=df['cat'].map({'cat1':'y', 'cat2':'g'}))

Output:

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top