我應該如何作為 Axis,Axes 或 Figure 在函式中傳遞 matplotlib 物件?


如需在函式中傳遞 matplotlib 物件;作為 Axis、Axes 或 figure,我們可以採取以下步驟 −

  • 設定圖形大小並調整子繪圖之間的和周圍的填充。
  • plot() 方法中,將 x 和 y 資料點繪製在軸 ax 上。
  • profile() 方法中,建立一個圖形和一組子繪圖。迭代軸並傳遞 plot() 方法以繪製圖形。
  • 使用 3 行和 4 列呼叫 profile() 方法。
  • 若要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

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

def plot(ax, x, y):
   ax.plot(x, y)

def profile(rows, cols):
   fig, axes = plt.subplots(rows, cols)
   for ax in axes:
      for a in ax:
plot(a, np.random.rand(10), np.random.rand(10))

profile(3, 4)
plt.show()

輸出

它將生成以下輸出

更新於: 22- 九月-2021

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告