Home > Blockchain >  Is there a better way to show different timeseries in one plot?
Is there a better way to show different timeseries in one plot?

Time:12-18

I have a table with three columns (Month_Year, SKU_ID, Actual_Demand)

I need to create a plot that showcases the movement in actual demand for each of my 10 SKU's.

The data is structured like this.

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

So far my method is to filter for each of the 10 SKU's and create a seperat plot.

# -*- 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)

But there has to be a better way than this?

I want to forecast based on each of the SKU later, when i have studied the data and it seems to me that i am not doing this smart.

CodePudding user response:

You can try using Seaborn and read up on the hue parameter.

For extra info, matplotlib is good for plotting 1-2variable while seaborn is great for more than 2 variables. In your case, you have 3 variables.

import seaborn as sns

#Assume ad is your DataFrame

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