A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/creating-a-time-series-plot-with-seaborn-and-pandas/ below:

Creating A Time Series Plot With Seaborn And Pandas

Creating A Time Series Plot With Seaborn And Pandas

Last Updated : 11 Dec, 2020

In this article, we will learn how to create A Time Series Plot With Seaborn And Pandas. Let's discuss some concepts :

Steps Needed
  1. Import packages
  2. Import / Load / Create data.
  3. Plot the time series plot over data using lineplot (as tsplot was replaced with lineplot since Sep 2020).
Examples

Here, we create a rough data for understanding the time series plot with the help of some examples. Let's create the data :

Python3
# importing packages
import pandas as pd

# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01',
                            '2020-06-01'],
                   
                   'Col_1': [34, 43, 14, 15,
                             15, 14, 31, 25, 62],
                   
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                   
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})

# view dataset
display(df)

Output:

Example 1: Simple time series plot with single column using lineplot

Python3
# importing packages
import seaborn as sns
import pandas as pd

# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                   
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                   
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                   
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})

# create the time series plot
sns.lineplot(x = "Date", y = "Col_1",
             data = df)

plt.xticks(rotation = 25)

Output :

Example 2: (Simple time series plot with multiple columns  using line plot)

Python3
# importing packages
import seaborn as sns
import pandas as pd

# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                   
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                   
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                   
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})

# create the time series plot
sns.lineplot(x = "Date", y = "Col_1", data = df)
sns.lineplot(x = "Date", y = "Col_2", data = df)
plt.ylabel("Col_1 and Col_2")
plt.xticks(rotation = 25)

Output :

Example 3: Multiple time series plot with multiple columns

Python3
# importing packages
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# creating data
df = pd.DataFrame({'Date': ['2019-10-01', '2019-11-01', 
                            '2019-12-01','2020-01-01', 
                            '2020-02-01', '2020-03-01',
                            '2020-04-01', '2020-05-01', 
                            '2020-06-01'],
                   
                   'Col_1': [34, 43, 14, 15, 15,
                             14, 31, 25, 62],
                   
                   'Col_2': [52, 66, 78, 15, 15,
                             5, 25, 25, 86],
                   
                   'Col_3': [13, 73, 82, 58, 52,
                             87, 26, 5, 56],
                   'Col_4': [44, 75, 26, 15, 15,
                             14, 54, 25, 24]})
# create the time series subplots
fig,ax =  plt.subplots( 2, 2,
                       figsize = ( 10, 8))

sns.lineplot( x = "Date", y = "Col_1", 
             color = 'r', data = df, 
             ax = ax[0][0])

ax[0][0].tick_params(labelrotation = 25)
sns.lineplot( x = "Date", y = "Col_2", 
             color = 'g', data = df,
             ax = ax[0][1])

ax[0][1].tick_params(labelrotation = 25)
sns.lineplot(x = "Date", y = "Col_3", 
             color = 'b', data = df,
             ax = ax[1][0])

ax[1][0].tick_params(labelrotation = 25)

sns.lineplot(x = "Date", y = "Col_4", 
             color = 'y', data = df, 
             ax = ax[1][1])

ax[1][1].tick_params(labelrotation = 25)
fig.tight_layout(pad = 1.2)

Output :


Similar Reads

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4