Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีได้ Gantt plot ด้วย matplotlib

Gantt charts ใช้กันอย่างแพร่หลายในการวางแผนโครงการแสดงตารางเวลา เป็นประเภท bar chart แสดง tasks บนแกน Y และช่วงเวลาบนแกน X ความกว้างของแท่งแนวนอนแสดงระยะเวลาแต่ละกิจกรรม

ในการวาด Gantt chart ใน matplotlib ใช้เมธอด broken_barh()

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้าง figure และชุด subplots

  • วาดลำดับสี่เหลี่ยมแนวนอน

  • ตั้งค่า y และ x limits ของ axes

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import matplotlib.pyplot as plt
# ตั้งค่าขนาดรูป
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Figure และ subplots
fig, ax = plt.subplots()
# Horizontal sequence of rectangles
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9), facecolors='tab:orange')
# ylim และ xlim ของ axes
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
# แสดง plot
plt.show()

ผลลัพธ์

จะแสดง Gantt chart สองแถว แถวบน 2 bars สีน้ำเงิน แถวล่าง 3 bars สีส้ม

วิธีได้ Gantt plot ด้วย matplotlib