Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - Create a Time Series Plot with multiple columns using Line Plot
To create a Time Series Plot with multiple columns using Line Plot, use Seaborn's lineplot() function. This allows you to visualize trends across time for different data series on the same plot.
Required Libraries
First, import the necessary libraries ?
import seaborn as sns import pandas as pd import matplotlib.pyplot as plt
Creating Sample Data
Create a DataFrame with date column and multiple numeric columns to plot ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame with sample sales data
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, 51, 70, 70, 87, 76],
'Units_Returned': [60, 50, 40, 57, 62, 51, 60]
})
# Convert date column to datetime
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])
print(dataFrame.head())
Date_of_Purchase Units_Sold Units_Returned 0 2018-07-25 98 60 1 2018-10-25 77 50 2 2019-01-25 51 40 3 2019-05-25 70 57 4 2019-08-25 70 62
Method 1: Multiple lineplot() Calls
Plot each series separately on the same axes ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame
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, 51, 70, 70, 87, 76],
'Units_Returned': [60, 50, 40, 57, 62, 51, 60]
})
# Convert to datetime
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])
# Create multiple line plots
plt.figure(figsize=(10, 6))
sns.lineplot(x="Date_of_Purchase", y="Units_Sold", data=dataFrame, marker='o', label='Units Sold')
sns.lineplot(x="Date_of_Purchase", y="Units_Returned", data=dataFrame, marker='s', label='Units Returned')
plt.title('Sales and Returns Over Time')
plt.ylabel('Number of Units')
plt.xlabel('Date')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Method 2: Using Melted Data
Reshape the DataFrame to long format for easier plotting ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame
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, 51, 70, 70, 87, 76],
'Units_Returned': [60, 50, 40, 57, 62, 51, 60]
})
# Convert to datetime
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])
# Melt the DataFrame
melted_df = dataFrame.melt(id_vars=['Date_of_Purchase'],
value_vars=['Units_Sold', 'Units_Returned'],
var_name='Metric', value_name='Count')
print(melted_df.head())
Date_of_Purchase Metric Count 0 2018-07-25 Units_Sold 98 1 2018-10-25 Units_Sold 77 2 2019-01-25 Units_Sold 51 3 2019-05-25 Units_Sold 70 4 2019-08-25 Units_Sold 70
Plotting Melted Data
Use the melted DataFrame with the hue parameter ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create and prepare data
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, 51, 70, 70, 87, 76],
'Units_Returned': [60, 50, 40, 57, 62, 51, 60]
})
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])
# Melt the DataFrame
melted_df = dataFrame.melt(id_vars=['Date_of_Purchase'],
value_vars=['Units_Sold', 'Units_Returned'],
var_name='Metric', value_name='Count')
# Create the plot
plt.figure(figsize=(10, 6))
sns.lineplot(data=melted_df, x='Date_of_Purchase', y='Count', hue='Metric', marker='o')
plt.title('Sales and Returns Time Series')
plt.ylabel('Number of Units')
plt.xlabel('Date')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Comparison
| Method | Best For | Advantages |
|---|---|---|
| Multiple lineplot() calls | Few columns | Simple, direct control over each line |
| Melted data with hue | Many columns | Automatic legend, consistent styling |
Conclusion
Use multiple lineplot() calls for simple cases with few columns. Use melted data with hue parameter for cleaner code when plotting many time series together.
Advertisements
