set_xlim − กำหนดขอบเขตมุมมอง (view limits) ของแกน X
set_xbound − กำหนดขอบเขตตัวเลข (numerical bounds) ล่างและบนของแกน X
ความแตกต่างหลักคือ set_xlim ควบคุมพื้นที่ที่มองเห็นได้ ในขณะที่ set_xbound ควบคุมช่วงข้อมูลที่จะแสดง และอาจมีผลต่อ autoscaling ต่างกัน
ขั้นตอนทดสอบ
- สร้าง Figure และ subplots 2 แถวด้วย subplots(2)
- สร้างข้อมูล x, y ด้วย NumPy
- พลอตบนแกนที่ 1 แล้วใช้ set_xlim(-3, 3)
- พลอตบนแกนที่ 2 แล้วใช้ set_xbound(-3, 3)
- แสดงกราฟด้วย show()
ตัวอย่างโค้ด
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, axes = plt.subplots(2) x = np.linspace(-5, 5, 100) y = np.sin(x) axes[0].plot(x, y, c='g') axes[0].set_xlim(-3, 3) axes[1].plot(x, y, c='r') axes[1].set_xbound(-3, 3) plt.show()
ผลลัพธ์
