Matplotlib - plt.subplots() 和 plt.figure() 之間


plt.figure() - 建立一個新圖形或啟用一個現有的圖形。

plt.subplots() - 建立一個圖形和一組子圖。

我們舉一個例子來了解 plt.subplots()plt.figure() 之間的區別。

步驟

  • 設定圖形大小並調整子圖之間和周圍的間距。

  • 建立一個新圖形或啟用一個現有的圖形。使用 plt.figure() 方法。

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

  • 要顯示圖形,請使用 Show() 方法。

例項

from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a new figure using plt.figure
fig1 = plt.figure("Figure 1 - plt.figure")

# Create a figure and set of subplots using plt.subplots
fig2, ax = plt.subplots()
plt.title("Figure 2 - plt.subplots")

# Display the plot
plt.show()

輸出

plt.figure() 產生以下輸出

plt.subplots() 產生以下輸出 -

更新於: 2021-10-11

2K+ 瀏覽

開啟您的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.