6.2 C
London
Thursday, December 19, 2024
HomeSoftware TutorialsPythonHow to Create a Time Series Plot in Seaborn

How to Create a Time Series Plot in Seaborn

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 seaborn 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.pyplot as 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)

Time series plot in seaborn

Example 2: Plot Multiple Time Series

The following code shows how to plot multiple time series in seaborn:

import pandas as pd
import matplotlib.pyplot as 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.

Additional Resources

The following tutorials explain how to perform other common functions in seaborn:

How to Add a Title to Seaborn Plots
How to Change Legend Font Size in Seaborn
How to Change the Position of a Legend in Seaborn

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories