Python - Create a Time Series Plot using Line Plot with Seaborn

A time series plot displays data points at successive time intervals. Seaborn's lineplot() function creates clean time series visualizations by connecting data points with lines to show trends over time.

Required Libraries

First, import the necessary libraries for data manipulation and visualization ?

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

Creating Sample Time Series Data

Create a DataFrame with date and numeric columns. The date column will serve as our time axis ?

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

# Create sample time series 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, 45, 70, 70, 87, 66]
})

print(dataFrame)
  Date_of_Purchase  Units_Sold
0       2018-07-25          98
1       2018-10-25          77
2       2019-01-25          45
3       2019-05-25          70
4       2019-08-25          70
5       2020-09-25          87
6       2021-03-25          66

Creating the Time Series Plot

Use lineplot() to create a time series visualization. Specify the date column as x-axis and numeric data as y-axis ?

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

# Create sample 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, 45, 70, 70, 87, 66]
})

# Create time series plot
sb.lineplot(x="Date_of_Purchase", y="Units_Sold", data=dataFrame)
plt.title("Units Sold Over Time")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Improving Date Formatting

For better date handling, convert the date column to datetime format before plotting ?

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

# Create sample 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, 45, 70, 70, 87, 66]
})

# Convert to datetime for better formatting
dataFrame['Date_of_Purchase'] = pd.to_datetime(dataFrame['Date_of_Purchase'])

# Create time series plot with markers
sb.lineplot(x="Date_of_Purchase", y="Units_Sold", data=dataFrame, marker='o')
plt.title("Sales Trend Analysis")
plt.ylabel("Units Sold")
plt.xlabel("Purchase Date")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Key Features

Parameter Description Example
x Time/date column "Date_of_Purchase"
y Numeric values to plot "Units_Sold"
marker Point markers 'o', 's', '^'
data DataFrame source dataFrame

Conclusion

Seaborn's lineplot() creates effective time series visualizations by connecting data points chronologically. Convert date columns to datetime format and add markers for better readability and professional appearance.

Updated on: 2026-03-26T13:40:51+05:30

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements