การเลื่อน Tick Label แยกจาก Tick Mark
ใช้ axvline() วาดเส้นแนวตั้ง แล้ว annotate() วาง label ตรงที่ต้องการ
ขั้นตอนหลัก
- ตั้งค่าขนาดรูปและ padding
- กำหนด delta ตำแหน่งที่ต้องการ
- สร้างข้อมูล x, y
- วาดเส้นแนวตั้งด้วย axvline(delta, ls="--", color="r")
- ทำ annotation ด้วย annotate() ระบุตำแหน่ง xy offset
- พล็อตกราฟหลัก
- แสดงผลด้วย show()
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
delta = 2.0
x = np.linspace(-10, 10, 100)
y = np.sinc(x - delta)
plt.axvline(delta, ls="--", color="r")
plt.annotate(r"$\delta$", xy=(delta + 0.2, -0.2), color="r", size=15)
plt.plot(x, y)
plt.show()
คำอธิบาย
axvline วาดเส้นอ้างอิง annotate วาง label ที่ตำแหน่งที่ต้องการ (offset จากเส้น) ไม่ผูกกับ tick system