Matplotlib 中的 axes.flat 做了什麼?


Axes.flat 表示對陣列進行 1D 迭代。透過一個示例演示 axes.flat 的用法。

步驟

  • 設定圖形大小和 subplot 之間以及周邊的填充。

  • 使用 subplots() 方法建立一個圖形和一組子圖。

  • 使用 numpy 建立 xy 資料點。

  • 使用 axes.flat 並遍歷所有軸(步驟 2)。

  • 使用 plot() 方法繪製 x 和 y 資料點。

  • 要顯示圖表,請使用 show() 方法。

示例

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

輸出

更新於: 2021-06-01

4K+ 瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告