如何在 Python 中繪製時間序列?


要使用 Matplotlib 在 Python 中繪製時間序列,我們可以採取以下步驟:

  • 使用 Numpy 建立 x 和 y 座標。

  • 使用 plot() 方法繪製建立的 x 和 y 點。

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

示例

import matplotlib.pyplot as plt
import datetime
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)])
y = np.random.randint(100, size=x.shape)
plt.plot(x, y)
plt.show()

輸出

更新時間: 09-Apr-2021

3K+ 瀏覽

開啟你的 職業生涯

透過完成課程來獲得認證

開始
廣告
© . All rights reserved.