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- 5 reasons to learn python
1. Dynamically Typed You don’t have to declare a type when declaring a variable. It skips headaches of type casting JAVA:- int x = 1; x = (int) x/2;...

Personalise touch of training & learning
We all are in countinous learning phase in life. Day to day we are learning new things,new language words,new faces,new gazets by all means of modes like reading books,newspapers,watching TV,movies,meeting...
S

Smartnub Softsolutions

0 0
0

Bigdata hadoop training institute in pune
BigData What is BigData Characterstics of BigData Problems with BigData Handling BigData • Distributed Systems Introduction to Distributed Systems Problems with Existing Distributed...

How Machine Learning Predict things - A Short Video for absolute beginners
Here is a short video for Machine Learning Beginners, who want to know how Machine Learning Algorithm predict things

Topic Modeling in Text Mining : LDA
Latent Dirichlet allocation (LDA) Topic modeling is a method for unsupervised classification of text documents, similar to clustering on numeric data, which finds natural groups of items even when we’re...
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