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

Vẽ lưới (grids) xuyên suốt các subplot trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo figure và subplots.
  3. Thêm subplot phụ (ax3) với zorder=-1, tắt spine visibility.
  4. Tắt nhãn trục ax3.
  5. Chia sẻ trục X giữa ax3, ax1.
  6. Cấu hình grid cho ax3 (trục X), ax1, ax2.
  7. Gọi show().

Ví dụ

import matplotlib.pyplot as plt

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

fig, (ax1, ax2) = plt.subplots(nrows=2)
ax3 = fig.add_subplot(111, zorder=-1)
for _, spine in ax3.spines.items():
    spine.set_visible(False)
ax3.tick_params(labelleft=False, labelbottom=False, left=False, right=False)
ax3.get_shared_x_axes().join(ax3, ax1)
ax3.grid(axis="x")
ax1.grid()
ax2.grid()
plt.show()

Kết quả

Vẽ lưới (grids) xuyên suốt các subplot trong Matplotlib