Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Sự khác biệt giữa set_xlim và set_xbound trong Matplotlib

set_xlim: đặt giới hạn hiển thị (view limits) của trục X.

set_xbound: đặt giới hạn số học (data bounds) của trục X.

Thường chúng giống nhau, nhưng set_xbound ảnh hưởng đến autoscaling, set_xlim không.

  • fig, axes = subplots(2).
  • x = linspace(-5,5,100), y = sin(x).
  • axes[0].plot(x,y,'g'), axes[0].set_xlim(-3,3).
  • axes[1].plot(x,y,'r'), axes[1].set_xbound(-3,3).
  • show().

Ví dụ

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()

Kết quả

Hai subplot trông giống nhau (xlim = xbound = [-3,3]), nhưng hành vi autoscale khác nhau nếu thêm dữ liệu mới.