ขั้นตอนการทำงาน
กำหนดขนาดรูปภาพและปรับระยะห่างรอบๆ subplots
สร้างข้อมูล x และ y ด้วย NumPy
พล็อตข้อมูลด้วย plot()
ตั้งค่าขอบเขต x และ y axes
แสดงรูปภาพด้วยเมธอด show()
ตัวอย่างโค้ด
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-10, 10, 100)
y = np.sin(x) ** 2
plt.plot(x, y)
plt.xlim([0, max(x) + 0.125])
plt.ylim([0, max(y) + 0.125])
plt.show()
ผลลัพธ์
กราฟ sin²(x) แสดงเฉพาะช่วง x ≥ 0 และ y ≥ 0 พร้อม Padding เล็กน้อย (0.125) ทั้งสองแกน
จุดสำคัญ: การตั้ง plt.rcParams["figure.autolayout"] = True (tight layout) อาจทำให้ Padding ถูกตัดออก การตั้งเป็น False จะรักษา Padding ไว้ได้ แต่ต้องจัด Layout เอง