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

Đặt cùng giới hạn trục cho tất cả subplot trong Matplotlib

Để đặt cùng giới hạn trục cho tất cả subplot, ta dùng subplot() với tham số sharex và sharey.

Các bước thực hiện

  • Thiết lập kích thước hình và khoảng đệm.
  • Thêm subplot index 1, đặt xlim, ylim, vẽ đường.
  • Thêm subplot index 2 với sharex=ax1, sharey=ax1, vẽ đường.
  • Thêm subplot index 3, 4 tương tự sharex, sharey.
  • Hiển thị bằng show().

Ví dụ

from matplotlib import pyplot as plt

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

ax1 = plt.subplot(2, 2, 1)
ax1.set_xlim(left=0, right=5)
ax1.set_ylim(bottom=0, top=5)
ax1.plot([1, 4, 3])

ax2 = plt.subplot(2, 2, 2, sharey=ax1, sharex=ax1)
ax2.plot([3, 4, 1])

ax3 = plt.subplot(2, 2, 4, sharey=ax1, sharex=ax1)
ax3.plot([2, 4, 2])

ax4 = plt.subplot(2, 2, 3, sharey=ax1, sharex=ax1)
ax4.plot([4, 0, 4])

plt.show()

Kết quả

Đặt cùng giới hạn trục cho tất cả subplot trong Matplotlib