如何用 Python 匯入 Matplotlib?


首先,確保系統中已預先安裝Pythonpip。要檢查Python 版本,請鍵入

python --version

要檢查 pip 版本,請鍵入

pip V

然後,在命令提示符中執行以下pip 命令來安裝 Matplotlib。

pip install matplotlib

要驗證系統是否已成功安裝 matplotlib,請在命令提示符中執行以下命令。

import matplotlib
matplotlib.__version__

如果 matplotlib 已成功安裝,它將顯示 matplotlib 的版本。

現在,匯入 Matplotlib,繪製一些隨機資料點。

步驟

  • 匯入 matplotlib
  • 設定影像大小,調整子圖之間及周圍的填充。
  • 建立隨機資料點 x
  • 使用 plot() 方法繪製 x
  • 要顯示該影像,使用 show() 方法。

例項

from matplotlib import pyplot as plt
import numpy as np

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

x = np.random.rand(20)
plt.plot(x, '*-', color='red', markersize=10)

plt.show()

輸出

更新時間:2023 年 8 月 23 日

78K+ 篇檢視

開啟你的職業生涯

完成課程即可獲得認證

開始吧
廣告
© . All rights reserved.