Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีพล็อต Time Series ใน Python (Matplotlib)


ขั้นตอนการพล็อต Time Series

  • สร้างจุด x และ y ด้วย NumPy
  • พล็อตจุด x และ y ที่สร้างขึ้นด้วยเมธอด plot()
  • ใช้เมธอด show() เพื่อแสดง Figure

ตัวอย่างโค้ด

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()

ผลลัพธ์

วิธีพล็อต Time Series ใน Python (Matplotlib)