匯入 matplotlib 和 matplotlib.pyplot 有什麼區別?


當我們匯入 matplotlib 時,即匯入了它所有的庫,而匯入 matplotlib.pyplot 僅匯入了 pyplot 的屬性。

步驟

  • matplotlib.pyplot 匯入為 plt

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

  • 使用 numpy 建立 x 和 y 資料點。

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

  • 使用 show() 方法來顯示圖形。

示例

import numpy as np
import matplotlib.pyplot as plt

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

x = np.random.rand(10)
y = np.random.rand(10)

plt.plot(x, y)

plt.show()

輸出

更新時間: 10-8-2021

1K+ 次瀏覽

Kickstart Your事業

完成課程,獲得認證

開始
廣告
© . All rights reserved.