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

Python in Unix
Python in Unix How to run command on unix Commands cannot be run directly on a unix server like we do in shell script. SYNTAX: system("Command") check_output("Command", shell=True) call("Command",...
R

Rishi B.

0 0
0
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