Open In App

Creating A Time Series Plot With Seaborn And Pandas

Last Updated : 11 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Pandas is an open-source library that’s built on top of NumPy library. It’s a Python package that gives various data structures and operations for manipulating numerical data and statistics. It’s mainly popular for importing and analyzing data much easier. Pandas is fast and it’s high-performance & productive for users.
  • Seaborn is a tremendous visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to form statistical plots more attractive. It’s built on the highest of matplotlib library and also closely integrated to the info structures from pandas.
  • A timeplot (sometimes called a statistic graph) displays values against the clock. They’re almost like x-y graphs, but while an x-y graph can plot a spread of “x” variables (for example, height, weight, age), timeplots can only display time on the x-axis. Unlike the pie charts and bar charts, these plots don’t have categories. Timeplots are good for showing how data changes over time. For instance, this sort of chart would work well if you were sampling data randomly times.

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 :



Previous Article
Next Article

Similar Reads

Time Series Plot or Line plot with Pandas
Prerequisite: Create a Pandas DataFrame from Lists Pandas is an open-source library used for data manipulation and analysis in Python. It is a fast and powerful tool that offers data structures and operations to manipulate numerical tables and time series. Examples of these data manipulation operations include merging, reshaping, selecting, data cl
6 min read
Pandas - Plot multiple time series DataFrame into a single plot
In this article, we are going to see how to plot multiple time series Dataframe into single plot. If there are multiple time series in a single DataFrame, you can still use the plot() method to plot a line chart of all the time series. To Plot multiple time series into a single plot first of all we have to ensure that indexes of all the DataFrames
4 min read
Pandas Series dt.time | Extract Time from Time Stamp in Series
The Series.dt.time attribute returns a NumPy array containing time values of the timestamps in a Pandas series. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime
2 min read
Plot Multiple Histograms On Same Plot With Seaborn
Histograms are a powerful tool for visualizing the distribution of data in a dataset. When working with multiple datasets or variables, it can be insightful to compare their distributions side by side. Seaborn, a python data visualization package offers powerful tools for making visually appealing maps and efficient way to plot multiple histograms
3 min read
Pandas Series dt.freq | Retrieve Frequency of Pandas Time Series
Pandas dt.freq attribute returns the time series frequency applied on the given series object if any, else it returns None. Examples C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_da
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series
The dt.normalize() method converts times to midnight. The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases when the time does not matter. Length is unaltered. The time zones are unaffected. Example: C/C++ Code import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'M',
2 min read
Box plot visualization with Pandas and Seaborn
Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used for detect the outlier in data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Boxplot summarizes a sample data using 25th, 50th and 75th per
2 min read
KDE Plot Visualization with Pandas and Seaborn
Kernel Density Estimate (KDE) plot, a visualization technique that offers a detailed view of the probability density of continuous variables. In this article, we will be using Iris Dataset and KDE Plot to visualize the insights of the dataset. What is KDE Plot?KDE Plot described as Kernel Density Estimate is used for visualizing the Probability Den
4 min read
How To Highlight a Time Range in Time Series Plot in Python with Matplotlib?
A time series plot is a plot which contains data which is being measured over a period of time, for example, a gross domestic product of a country, the population of the world and many other data. Sometimes we want to highlight a specific period of the timeline so that it is easier for the observer to read specific data. We can highlight a time ran
4 min read
Pandas Series dt.weekofyear Method | Get Week of Year in Pandas Series
The dt.weekofyear attribute returns a Series containing the week ordinal of the year in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index =
2 min read
Practice Tags :