Table of Contents
Seaborn is a popular Python data visualization library that allows for the creation of professional-looking charts and plots. One of the useful features of Seaborn is its ability to create time series plots. Time series plots are a graphical representation of data points collected over a period of time. To create a time series plot using Seaborn, first, the data needs to be organized in a proper format, with a time column and a variable column. Then, the data can be passed to Seaborn’s lineplot function, which will automatically generate a plot with the time as the x-axis and the variable as the y-axis. Seaborn also offers various customization options, such as changing the color and style of the line, adding labels and titles, and incorporating multiple variables into one plot. Overall, Seaborn’s simple syntax and powerful capabilities make it a popular choice for creating time series plots in data analysis and research.
Create a Time Series Plot in Seaborn
A time series plot is useful for visualizing data values that change over time.
This tutorial explains how to create various time series plots using the data visualization package in Python.
Example 1: Plot a Single Time Series
The following code shows how to plot a single time series in seaborn:
import pandas as pd import matplotlib.pyplotas plt import seaborn as sns #create DataFrame df = pd.DataFrame({'date': ['1/2/2021', '1/3/2021', '1/4/2021', '1/5/2021', '1/6/2021', '1/7/2021', '1/8/2021'], 'value': [4, 7, 8, 13, 17, 15, 21]}) sns.lineplot(x='date', y='value', data=df)

Note that we can also customize the colors, line width, line style, labels, and titles of the plot:
#create time series plot with custom aesthetics sns.lineplot(x='date', y='value', data=df, linewidth=3, color='purple', linestyle='dashed').set(title='Time Series Plot') #rotate x-axis labels by 15 degrees plt.xticks(rotation=15)

Example 2: Plot Multiple Time Series
The following code shows how to plot multiple time series in seaborn:
import pandas as pd import matplotlib.pyplotas plt import seaborn as sns #create DataFrame df = pd.DataFrame({'date': ['1/1/2021', '1/2/2021', '1/3/2021', '1/4/2021', '1/1/2021', '1/2/2021', '1/3/2021', '1/4/2021'], 'sales': [4, 7, 8, 13, 17, 15, 21, 28], 'company': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B']}) #plot multiple time series sns.lineplot(x='date', y='sales', hue='company', data=df)

Note that the hue argument is used to provide different colors to each line in the plot.
The following tutorials explain how to perform other common functions in seaborn:
Cite this article
stats writer (2024). How can a time series plot be created using Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-time-series-plot-be-created-using-seaborn/
stats writer. "How can a time series plot be created using Seaborn?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-a-time-series-plot-be-created-using-seaborn/.
stats writer. "How can a time series plot be created using Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-time-series-plot-be-created-using-seaborn/.
stats writer (2024) 'How can a time series plot be created using Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-time-series-plot-be-created-using-seaborn/.
[1] stats writer, "How can a time series plot be created using Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can a time series plot be created using Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
