UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Code: Gantt Chart: Horizontal bar using matplotlib for tasks with Start Time and End Time

R
Rishi B.
08/04/2020 0 0

import pandas as pd   
from datetime import datetime
import matplotlib.dates as dates
import matplotlib.pyplot as plt

 

def gantt_chart(df_phase):

    # Now convert them to matplotlib's internal format...

    df_phase['Start Time'] = pd.to_datetime(df_phase['Start Time'], format='%Y-%m-%d %H:%M:%S.%f')

    df_phase['End Time'] = pd.to_datetime(df_phase['End Time'], format='%Y-%m-%d %H:%M:%S.%f')

    #Convert DF columns into lists, plt will take all values in scalar or list

    sdate = df_phase['Start Time'].tolist()

    edate = df_phase['End Time'].tolist()

    plugin = df_phase['Plugin'].tolist()

    status = df_phase['Status'].tolist()

    color = []

    #Store colors for success and failure to differentiate

    for i in range(0, len(status)):

        if status[i] == 'Success':

            color.append('green')

        else:

            color.append('red')

    #Convert time to Matplotlib number format

    edate, sdate = [dates.date2num(item) for item in (edate, sdate)]

    ypos = range(len(plugin))

    fig, ax = plt.subplots()

    time_diff = edate - sdate

    # Plot the data, color is scalar or a list

    # All are in form of list

    ax.barh(ypos, time_diff, left=sdate, linewidth = 0.5, height=0.8, align='center', color=color)

    plt.yticks(ypos, plugin)

    plt.xticks(rotation=-20)

    ax.axis('tight')

 

    # We need to tell matplotlib that these are dates...

    ax.xaxis_date()

    plt.ylabel('Plugins', fontsize='medium', stretch = 'normal')

    # bbox_inches='tight' will prevent cutting of y-label

    fig.savefig(image_file, bbox_inches='tight')

    plt.show()

 

 

data = [['A', '2019-06-27 18:33:58.033', '2019-06-27 19:54:04.658', 'Success'], ['B', '2019-06-27 19:54:04.957', '2019-06-27 19:58:14.570', 'Success'], ['C', '2019-06-27 19:54:04.963', '2019-06-27 19:54:19.928', 'Failed']]

df_phase = pd.DataFrame(data, columns = [Plugin, 'Start Time', 'End Time', 'Status'])

#Calling the function

gantt_chart(df_phase)

 

Note: This is authored my me only, you can find it on my blog.

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Decorator Introduction
A decorator takes in a function, adds some functionality and returns it. Functions can be passed as arguments to another function like map, filter and reduce Function that take other functions as arguments...

What is M.S.Project ?
MICROSOFT PROJECT contains project work and project groups, schedules and finances.Microsoft Project permits its users to line realistic goals for project groups and customers by making schedules, distributing...

Why Do People Choose Python?
Lot of programming languages are available in the market today, but why do people choose python over them? What made it different from other scirpting languages. So factors made it unique, as below: Software...

Getting started with Python IDLE
Now that we have looked at some example programs, we would like to learn how to begin the actual process of writing a program in Python. There are a variety of ways to tell Python to execute the code we...

A python program using the datetime module
Task : To write a program in Python to find out, in any given year, Friday the 13th dates i.e 13th day of a month which was a Friday. ...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more