Python+甘特图及标签设置

图示

Python+甘特图及标签设置_第1张图片

甘特图代码

import matplotlib.pyplot as plt
import numpy as np


class ProjectEmement:
    def __init__(self, name_, starttime_: float, endtime_: float, fact_endtime_: float, grade_, rootlist_: list, keylist_: list, isover_=-1):
        self.name = name_
        self.starttime = starttime_
        self.endtime = endtime_
        self.fact_endtime = fact_endtime_
        self.grade = grade_
        self.rootlist = rootlist_
        self.keylist = keylist_
        self.isover = isover_

        # 预处理
        self.preprocess()
        pass

    def preprocess(self):
        self.starttime = min(self.starttime, self.endtime)
        self.fact_endtime = max(self.starttime, self.fact_endtime)
        self.costtime = self.fact_endtime - self.starttime
        pass


class GanttImage:
    def __init__(self, elementlist_: list):
        self.elementlist = elementlist_
        self.get_gant_image()
        pass

    def get_gant_csv(self):
        pass

    def get_gant_tree(self):
        pass

    def get_gant_image(self):
        self.bottomvalue = []
        self.innervalue = []
        self.topvalue = []
        self.tick_label = []
        self.text = []
        self.idx = []
        idx = 0
        for k in self.elementlist:
            tmp = [k.starttime, k.endtime, k.fact_endtime]
            self.bottomvalue.append(tmp[0])
            self.innervalue.append(tmp[1])
            self.topvalue.append(tmp[2])
            self.tick_label.append(k.name)
            self.text.append(",".join(k.keylist))
            self.idx.append(idx)
            idx += 1
        pass


def plot_gannt(pltimage: GanttImage):
    # 设置绘图风格
    plt.style.use('ggplot')
    # 处理中文乱码
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    for k in pltimage.idx:
        # 开始时间选择white
        # 实际结束时间设置为红色,black
        # 原始结束时间设置为蓝色,blue
        # 选择绘图顺利
        image_color = ["white", "black", "red"]
        pltvalue = [pltimage.bottomvalue[k],
                    pltimage.innervalue[k], pltimage.topvalue[k]]
        sorted_id = sorted(range(len(pltvalue)),
                           key=lambda mm: pltvalue[mm], reverse=True)
        # 绘制甘特图
        for kk in sorted_id:
            plt.barh(y=k, width=pltvalue[kk],
                     height=0.1, color=image_color[kk])
        plt.text(max(pltvalue), k, pltimage.text[k], ha="left", va='center')
        plt.xticks(range(0, 13), [str((k % 12)+1)+"月" for k in range(0, 13)])

        # 绘制指示文本
        plt.barh(y=pltimage.idx, width=pltimage.bottomvalue, height=0.1,
                 color=image_color[0], tick_label=pltimage.tick_label)
    plt.show()
    pass


def help_example():
    e0 = ProjectEmement("任务1", 1, 3, 2, 1, ["什么"], ["内容"], 1)
    e1 = ProjectEmement("任务2", 6, 8, 9, 1, ["为什么"], ["这样写"], 1)
    e2 = ProjectEmement("任务3", 10, 11, 12, 1, ["好"], ["的"], 1)
    plot_gannt(GanttImage([e0, e1, e2]))
    pass


def help_example0():

    # 设置绘图风格
    plt.style.use('ggplot')
    # 处理中文乱码
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    # 绘制条形图
    idx = list(range(10))
    plt.barh(y=idx,
             width=idx,
             height=0.1,  # 指定条形图y轴的数值(python3.7不能用y,而应该a用height)
             tick_label=idx,  # 指定条形图x轴的刻度标签
             color='steelblue'  # 指定条形图的填充色
             )
    plt.barh(y=idx,
             width=[k*0.8 for k in idx],
             height=0.1,  # 指定条形图y轴的数值(python3.7不能用y,而应该用height)
             color='white'  # 指定条形图的填充色
             )

    plt.show()

    pass


if __name__ == "__main__":
    help_example()
    pass

···

# 注意事项
利用柱状图设计

你可能感兴趣的:(python,甘特图,开发语言)