图示
甘特图代码
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:
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,
tick_label=idx,
color='steelblue'
)
plt.barh(y=idx,
width=[k*0.8 for k in idx],
height=0.1,
color='white'
)
plt.show()
pass
if __name__ == "__main__":
help_example()
pass
···
利用柱状图设计