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

วิธีสร้างลูกศรสองหัว (Double-headed Arrow) บนแกนใน Matplotlib

การสร้างลูกศรสองหัวบนแกนพิกัด

ใช้ annotate() พร้อม arrowstyle='<->'

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • ใช้ annotate() ระบุ xy (ปลาย), xytext (ต้น), arrowprops
  • ใน arrowprops: arrowstyle='<->' (สองหัว), color='red'
  • แสดงผลด้วย show()

ตัวอย่างโค้ด

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

plt.annotate('Arrows', xy=(0.1, .1), xytext=(0.5, 0.5),
             arrowprops=dict(arrowstyle='<->', color='red'))
plt.show()

Arrow Styles อื่นๆ

  • ->: ลูกศรหัวเดียว
  • -[: bracket
  • fancy: ลูกศรสวยงาม
  • simple: ลูกศรแบบง่าย