Define a class that draws a Gantt chart
# -*- coding: utf-8 -*- from datetime import datetime import sys import numpy as np import as plt import matplotlib.font_manager as font_manager import as mdates import logging from pylab import * ['-serif'] = ['SimHei'] class Gantt(object): #Color swatches: reference/ RdYlGr = ['#d73027', '#f46d43', '#fdae61','#fee08b', '#ffffbf', '#d9ef8b','#a6d96a', '#66bd63', '#1a9850'] POS_START = 1.0 POS_STEP = 0.5 def __init__(self, tasks): self._fig = (figsize=(15,10)) self._ax = self._fig.add_axes([0.1, 0.1, .75, .5]) = tasks[::-1] # Reverse order def _format_date(self, date_string): try: date = (date_string, '%Y-%m-%d %H:%M:%S') # Convert a date string to a datetime type except ValueError as err: ("String '{0}' can not be converted to datetime object: {1}" .format(date_string, err)) (-1) mpl_date = mdates.date2num(date) # Get a date type timestamp return mpl_date def _plot_bars(self): i = 0 for task in : start = self._format_date(task['start']) # Get the timestamp of the task start time end = self._format_date(task['end']) # Get the timestamp of the end time of the task bottom = (i * Gantt.POS_STEP) + Gantt.POS_START width = end - start # Width of column self._ax.barh(bottom, width, left=start, height=0.3,align='center', label=task['label'],color = [i%len()]) i += 1 def _configure_yaxis(self): task_labels = [t['label'] for t in ] # All the scale text labels pos = self._positions(len(task_labels)) # Vegetative scale values ylocs = self._ax.set_yticks(pos) # Setting the y-axis scale ylabels = self._ax.set_yticklabels(task_labels) # Set y-axis scale labels (ylabels, size='medium') # Set y-axis scale label attribute (medium font) def _configure_xaxis(self): self._ax.xaxis_date() # Using the timeline rule = (, interval=1) # Generate time generator (1 value per week, starting on Sunday) loc = (rule) # Generate time scales formatter = ("%m/%d") # Generate time format self._ax.xaxis.set_major_locator(loc) # Setting the master scale self._ax.xaxis.set_major_formatter(formatter) # Setting up the main scale label format xlabels = self._ax.get_xticklabels() # Get the scale label object (xlabels, rotation=70, fontsize=10) # Set the properties of the scale label object (30 degree rotation, font size 10) def _configure_figure(self): self._configure_xaxis() self._configure_yaxis() self._ax.grid(True, axis='x',color='gray') self._set_legend() self._fig.autofmt_xdate() def _set_legend(self): font = font_manager.FontProperties(size='small') self._ax.legend(loc='upper right', prop=font) def _positions(self, count): end = count * Gantt.POS_STEP + Gantt.POS_START pos = (Gantt.POS_START, end, Gantt.POS_STEP) return pos def show(self): self._plot_bars() self._configure_figure() ()
Calls and data formats
if __name__ == '__main__': TEST_DATA = ( { 'label': 'Project Research', 'start':'2019-02-01 12:00:00', 'end': '2019-03-15 18:00:00'}, { 'label': 'Project preparation', 'start':'2019-02-15 09:00:00', 'end': '2019-04-09 12:00:00'}, { 'label': 'Developing programs', 'start':'2019-04-10 12:00:00', 'end': '2019-05-30 18:00:00'}, { 'label': 'Project implementation', 'start':'2019-05-01 09:00:00', 'end': '2019-08-31 13:00:00'}, { 'label': 'Project training', 'start':'2019-07-01 09:00:00', 'end': '2019-09-21 13:00:00'}, { 'label': 'Project acceptance', 'start':'2019-09-22 09:00:00', 'end': '2019-10-22 13:00:00'}, { 'label': 'Project completion', 'start':'2019-10-23 09:00:00', 'end': '2019-11-23 13:00:00'}, ) gantt = Gantt(TEST_DATA) ('Project date') ('Project progress') ('Gantt chart of project progress') (figsize=(10,10),dpi=150) ()
A graphic similar to the one shown
To this article on matplotlib Gantt chart of the universal template case is introduced to this article, more related matplotlib Gantt chart content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!