ตั้งค่า Column เวลาเป็น Index แล้ว Plot โดยตรงจาก DataFrame
ขั้นตอน
- ตั้งค่าขนาด Figure
- สร้าง DataFrame มีคอลัมน์
timeและspeed - ตั้ง Index ด้วย
df.set_index('time') - เรียก
.plot()บน DataFrame - แสดงผล
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame(dict(
time=list(pd.date_range("2021-01-01 12:00:00", periods=10)),
speed=np.linspace(1, 10, 10)
))
df.set_index('time').plot()
plt.show()ผลลัพธ์
กราฟเส้น Time Series แสดง Speed ตามเวลา
