matplotlib繪制甘特圖的萬能模板案例
更新時間:2022年04月13日 09:43:51 作者:王小王-123
matplotlib是常見的繪圖庫,本文主要介紹了matplotlib繪制甘特圖的萬能模板案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
定義一個繪制甘特圖的類
# -*- 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': '項目調(diào)研', 'start':'2019-02-01 12:00:00', 'end': '2019-03-15 18:00:00'}, { 'label': '項目準(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': '項目實施', 'start':'2019-05-01 09:00:00', 'end': '2019-08-31 13:00:00'}, { 'label': '項目培訓(xùn)', 'start':'2019-07-01 09:00:00', 'end': '2019-09-21 13:00:00'}, { 'label': '項目驗收', 'start':'2019-09-22 09:00:00', 'end': '2019-10-22 13:00:00'}, { 'label': '項目竣工', 'start':'2019-10-23 09:00:00', 'end': '2019-11-23 13:00:00'}, ) gantt = Gantt(TEST_DATA) plt.xlabel('項目日期') plt.ylabel('項目進(jìn)度') plt.title('項目進(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-04python調(diào)用帶空格的windows?cmd命令問題及連續(xù)運行多個命令方式
這篇文章主要介紹了python調(diào)用帶空格的windows?cmd命令問題及連續(xù)運行多個命令方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02python 使用tkinter+you-get實現(xiàn)視頻下載器
這篇文章主要介紹了python 使用tkinter+you-get實現(xiàn)視頻下載器,幫助大家方便的下載視頻資源,感興趣的朋友可以了解下2020-11-11Python操作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