【问题标题】:Is there a better way to show different timeseries in one plot?有没有更好的方法在一个图中显示不同的时间序列?
【发布时间】:2022-01-20 13:24:44
【问题描述】:

我有一个包含三列(Month_Year、SKU_ID、Actual_Demand)的表

我需要创建一个图来展示我的 10 个 SKU 中每个 SKU 的实际需求变化。

数据的结构是这样的。

Month_Year SKU_ID Actual_Demand
Jan-2015 1 56
Feb-2015 2 70
Jan-2016 1 23
Jan-2016 2 56
Dec-2019 10 100

到目前为止,我的方法是过滤 10 个 SKU 中的每一个并创建一个单独的图。

# -*- coding: utf-8 -*-
"""
Created on Thu Sep  9 14:31:10 2021

@author: D996FFO
"""


'Importing Relevant Packages'

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



'Load in dataset with historical sales values'
ad = pd.read_excel (r'C:/Users/d996ffo/Model_Data.xlsx', sheet_name= 'Data_For_Python')


#SKU 1
sku_1 = ad.loc[ad['SKU_ID'] == 1]

'Converting the string format month year into a date format'
sku_1['Month_Year'] = pd.to_datetime(sku_1['Month_Year'])


'Set index equal to the date'
sku_1.index = sku_1['Month_Year']
del sku_1['Month_Year']
del sku_1['SKU_ID']

#SKU 2

sku_2 = ad.loc[ad['SKU_ID'] == 2]

'Converting the string format month year into a date format'
sku_2['Month_Year'] = pd.to_datetime(sku_2['Month_Year'])


'Set index equal to the date'
sku_2.index = sku_2['Month_Year']
del sku_2['Month_Year']
del sku_2['SKU_ID']


plt.plot(sku_1, color = 'blue', label = 'SKU 1')
plt.plot(sku_2, color = 'red', label = 'SKU 2')

sns.lineplot(data = sku_2.Actual_Demand)

但一定有比这更好的方法吗?

稍后我想根据每个 SKU 进行预测,当我研究了数据后,在我看来我做的并不聪明。

【问题讨论】:

  • ad = pd.read_excel(...); sns.lineplot(data=ad, x='Month_Year', y='Actual_Demand', hue='SKU_ID')
  • 这很有帮助,谢谢!

标签: python pandas time-series


【解决方案1】:

您可以尝试使用 Seaborn 并阅读色调参数。

有关额外信息,matplotlib 适合绘制 1-2 变量,而 seaborn 适合绘制 2 个以上的变量。在您的情况下,您有 3 个变量。

import seaborn as sns

#Assume ad is your DataFrame

sns.lineplot(data=ad, x="Month_Year", y="Actual_Demand", hue="SKU_ID")
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多