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

Python - สร้าง Time Series Plot ด้วย Line Plot ใน Seaborn

บทนำ

Time Series Plot ใช้ lineplot() แสดงข้อมูลตามเวลา สร้าง DataFrame มีคอลัมน์ Date_of_Purchase และ Units Sold

ขั้นตอน

  1. นำเข้า seaborn, pandas, matplotlib
  2. สร้าง DataFrame ข้อมูลวันที่และยอดขาย
  3. วาด Time Series Plot:
    sb.lineplot(x="Date_of_Purchase", y="Units Sold", data=dataFrame)

โค้ดเต็ม

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

dataFrame = pd.DataFrame({
    'Date_of_Purchase': ['2018-07-25', '2018-10-25', '2019-01-25', 
                         '2019-05-25', '2019-08-25', '2020-09-25', '2021-03-25'],
    'Units Sold': [98, 77, 45, 70, 70, 87, 66]
})

sb.lineplot(x="Date_of_Purchase", y="Units Sold", data=dataFrame)
plt.show()

ผลลัพธ์

ได้ Time Series Line Plot แสดงยอดขายตามเวลา