ใช้ ax.broken_barh() วาดแท่งแนวนอนที่斷續 (Broken) หลายช่วงในแต่ละ Row
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
# Row 1: 2 bars at y=10-19 (height 9)
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
# Row 2: 3 bars at y=20-29, different colors
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
facecolors=('tab:orange', 'tab:green', 'tab:red'))
# Axis limits
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
# Grid
ax.grid(True)
# Annotation
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16, horizontalalignment='right', verticalalignment='top')
plt.show()
คำอธิบาย
broken_barh(xranges, yrange, facecolors)xranges: List of tuples[(xmin, width), ...]yrange: Tuple(ymin, height)ตำแหน่งและสูงของ Rowfacecolors: สีเดียวสำหรับทุกแท่ง หรือ Tuple สีต่างกันต่อแท่ง- เหมาะสำหรับ Gantt Chart หรือ Timeline Visualization