matplotlib繪制甘特圖的萬能模板案例
定義一個繪制甘特圖的類
# -*- coding: utf-8 -*-
from datetime import datetime
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.dates as mdates
import logging
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
class Gantt(object):
#顏色色標(biāo):參考http://colorbrewer2.org/
RdYlGr = ['#d73027', '#f46d43', '#fdae61','#fee08b', '#ffffbf', '#d9ef8b','#a6d96a', '#66bd63', '#1a9850']
POS_START = 1.0
POS_STEP = 0.5
def __init__(self, tasks):
self._fig = plt.figure(figsize=(15,10))
self._ax = self._fig.add_axes([0.1, 0.1, .75, .5])
self.tasks = tasks[::-1] # 倒序
def _format_date(self, date_string):
try:
date = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') # 將日期字符串轉(zhuǎn)換成datetime類型
except ValueError as err:
logging.error("String '{0}' can not be converted to datetime object: {1}"
.format(date_string, err))
sys.exit(-1)
mpl_date = mdates.date2num(date) # 得到日期類型的時間戳
return mpl_date
def _plot_bars(self):
i = 0
for task in self.tasks:
start = self._format_date(task['start']) # 獲取任務(wù)開始時間的時間戳
end = self._format_date(task['end']) # 獲取任務(wù)結(jié)束時間的時間戳
bottom = (i * Gantt.POS_STEP) + Gantt.POS_START
width = end - start # 柱子的寬度
self._ax.barh(bottom, width, left=start, height=0.3,align='center', label=task['label'],color = Gantt.RdYlGr[i%len(Gantt.RdYlGr)])
i += 1
def _configure_yaxis(self):
task_labels = [t['label'] for t in self.tasks] # 所有的刻度文本標(biāo)簽
pos = self._positions(len(task_labels)) # 素有的刻度值
ylocs = self._ax.set_yticks(pos) # 設(shè)置y軸刻度線
ylabels = self._ax.set_yticklabels(task_labels) # 設(shè)置y軸刻度標(biāo)簽
plt.setp(ylabels, size='medium') # 設(shè)置y軸刻度標(biāo)簽屬性(中號字)
def _configure_xaxis(self):
self._ax.xaxis_date() # 使用時間軸
rule = mdates.rrulewrapper(mdates.WEEKLY, interval=1) # 生成時間生成器(每周1個值,從周日開始)
loc = mdates.RRuleLocator(rule) # 生成時間刻度
formatter = mdates.DateFormatter("%m/%d") # 生成時間格式
self._ax.xaxis.set_major_locator(loc) # 設(shè)置主刻度
self._ax.xaxis.set_major_formatter(formatter) # 設(shè)置主刻度標(biāo)簽格式
xlabels = self._ax.get_xticklabels() # 獲取刻度標(biāo)簽對象
plt.setp(xlabels, rotation=70, fontsize=10) # 設(shè)置刻度標(biāo)簽對象的屬性(30度旋轉(zhuǎn),字體大小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 = np.arange(Gantt.POS_START, end, Gantt.POS_STEP)
return pos
def show(self):
self._plot_bars()
self._configure_figure()
plt.show()調(diào)用及數(shù)據(jù)格式
if __name__ == '__main__':
TEST_DATA = (
{ 'label': '項(xiàng)目調(diào)研', 'start':'2019-02-01 12:00:00', 'end': '2019-03-15 18:00:00'},
{ 'label': '項(xiàng)目準(zhǔn)備', 'start':'2019-02-15 09:00:00', 'end': '2019-04-09 12:00:00'},
{ 'label': '制定方案', 'start':'2019-04-10 12:00:00', 'end': '2019-05-30 18:00:00'},
{ 'label': '項(xiàng)目實(shí)施', 'start':'2019-05-01 09:00:00', 'end': '2019-08-31 13:00:00'},
{ 'label': '項(xiàng)目培訓(xùn)', 'start':'2019-07-01 09:00:00', 'end': '2019-09-21 13:00:00'},
{ 'label': '項(xiàng)目驗(yàn)收', 'start':'2019-09-22 09:00:00', 'end': '2019-10-22 13:00:00'},
{ 'label': '項(xiàng)目竣工', 'start':'2019-10-23 09:00:00', 'end': '2019-11-23 13:00:00'},
)
gantt = Gantt(TEST_DATA)
plt.xlabel('項(xiàng)目日期')
plt.ylabel('項(xiàng)目進(jìn)度')
plt.title('項(xiàng)目進(jìn)度甘特圖')
plt.figure(figsize=(10,10),dpi=150)
gantt.show()類似于展示的圖形

到此這篇關(guān)于matplotlib繪制甘特圖的萬能模板案例的文章就介紹到這了,更多相關(guān)matplotlib 甘特圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
已解決不小心卸載pip后怎么處理(重新安裝pip的兩種方式)
這篇文章主要介紹了已解決不小心卸載pip后怎么處理(重新安裝pip的兩種方式),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
python調(diào)用帶空格的windows?cmd命令問題及連續(xù)運(yùn)行多個命令方式
這篇文章主要介紹了python調(diào)用帶空格的windows?cmd命令問題及連續(xù)運(yùn)行多個命令方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
利用Python實(shí)現(xiàn)自定義連點(diǎn)器
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)自定義連點(diǎn)器,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
python3.4實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了python3.4實(shí)現(xiàn)郵件發(fā)送功能,含帶中文附件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
python 使用tkinter+you-get實(shí)現(xiàn)視頻下載器
這篇文章主要介紹了python 使用tkinter+you-get實(shí)現(xiàn)視頻下載器,幫助大家方便的下載視頻資源,感興趣的朋友可以了解下2020-11-11
Python操作MySQL數(shù)據(jù)庫的基本方法(查詢與更新)
在工作中我們需要經(jīng)常對數(shù)據(jù)庫進(jìn)行操作,比如 Oracle、MySQL、SQL Sever等,這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫的基本方法包括了數(shù)據(jù)查詢與數(shù)據(jù)更新(新增、刪除、修改),需要的朋友可以參考下2023-09-09
pandas求平均數(shù)和中位數(shù)的方法實(shí)例
pandas對象擁有一組常用的數(shù)學(xué)和統(tǒng)計(jì)方法,大部分都屬于約簡和匯總統(tǒng)計(jì),這篇文章主要給大家介紹了關(guān)于pandas求平均數(shù)和中位數(shù)的相關(guān)資料,需要的朋友可以參考下2021-08-08

