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

map function in python
The function map takes a function and an iterable as arguments and returns a new iterable with the function applied to each argument. Example: def add_five(x): return x+5 nums = result = list(map(add_five,...

What is a generator?
A generator is a function that has one or more yield statements. Example: >>>def gen_demo(a): yield a a = a+10 yield a a = a+30 yield a >>>gen_a...

Top Programming Languages 2017: Every Beginner Should Learn
Every year a plethora of job opportunities are being created for skilled programmers. So if you are thinking of honing your coding skills it is really a bright idea. But with so many programming languages...

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

Two lists using Loops - Python Program 1
To generate a list of all possible combinations of two lists using loops, you can use nested loops to iterate through both lists and create a new list that contains all the combinations. list1 = list2...
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