ภาพรวม
ใช้ axhline() บนแต่ละ Axes ของ Subplots เพื่อวาดเส้นแนวนอน
ขั้นตอน
- สร้าง Figure และ Subplots (3 แถว)
- บนแต่ละ Ax เรียก
axhline(y=..., xmin=..., xmax=..., c=..., linewidth=..., zorder=...) - แสดงผล
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(3)
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
ax1.axhline(y=0.5, xmin=0, xmax=3, c="black", linewidth=2, zorder=0)
ax2.axhline(y=0.5, xmin=0, xmax=3, c="red", linewidth=3, zorder=0)
ax3.axhline(y=0.5, xmin=0, xmax=3, c="yellow", linewidth=4, zorder=0)
plt.show()
พารามิเตอร์ axhline
y: ตำแหน่ง Y ของเส้นxmin, xmax: ช่วง X (0-1 แบบ Relative)c/color: สีเส้นlinewidth/lw: ความหนาzorder: ลำดับชั้น (ต่ำ = ใต้ข้อมูล)
ฟังก์ชันเกี่ยวข้อง
axvline(): เส้นแนวตั้งaxhspan(),axvspan(): พื้นที่แนวนอน/แนวตั้ง