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

axes.flat trong Matplotlib làm gì?

Axes.flat là một iterator 1D trên mảng các axes. Hãy xem ví dụ để hiểu cách dùng axes.flat.

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

  • Thiết lập kích thước hình và khoảng đệm.
  • Tạo hình và tập subplots bằng subplots().
  • Tạo dữ liệu x, y bằng NumPy.
  • Dùng axes.flat để lặp qua tất cả axes.
  • Vẽ x, y bằng plot() trên mỗi axes.
  • Hiển thị bằng show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt

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

fig, axes = plt.subplots(nrows=2, ncols=3)
x = np.random.rand(10)
y = np.random.rand(10)

for _, ax in enumerate(axes.flat):
    ax.plot(x, y)

plt.show()

Kết quả

axes.flat trong Matplotlib làm gì?