ในการทำเครื่องหมาย (annotate) ช่วงของแกน X ใน matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้:
- ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
- สร้างจุดข้อมูล xx และ yy ด้วย numpy
- สร้าง figure และชุด subplots
- วาดจุดข้อมูล xx และ yy ด้วยเมธอด plot()
- ตั้งค่า ylim ของแกน
- ใช้เมธอด annotate เพื่อวางหัวลูกศรและชื่อช่วง
- เพื่อแสดงรูป ใช้เมธอด show()
ตัวอย่าง
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
xx = np.linspace(0, 10)
yy = np.sin(xx)
fig, ax = plt.subplots(1, 1)
ax.plot(xx, yy)
ax.set_ylim([-2, 2])
ax.annotate('', xy=(5,2), xytext=(8,2),
xycoords='data', textcoords='data',
arrowprops={'arrowstyle': '<|-|>'}, color='yellow')
ax.annotate('Maximum Range', xy=(5,5), ha='center', va='center', color='red')
plt.show()
ผลลัพธ์

