Open In App

How to Plot a Time Series in Matplotlib?

Time series data is the data marked by some time. Each point on the graph represents a measurement of both time and quantity.  A time-series chart is also known as a fever chart when the data are connected in chronological order by a straight line that forms a succession of peaks and troughs. x-axis of the chart is used to represent time intervals. y-line locates values of the parameter getting monitored.

We will use the syntax mentioned below to draw a Time Series graph:



Syntax:

plt.plot(dataframe.X, dataframe.Y)

where



We can also rotate the axis by using xticks() function

Syntax:

plt.xticks(rotation, ha)

where

Approach

Example 1:

Let say we have a dataframe of the days of the week and the number of classes on each day of the upcoming week. We are  Taking 7 days from 1-11-2021 to 7-11-2021




# import modules
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
 
# create dataframe
 
dataframe = pd.DataFrame({'date_of_week': np.array([datetime.datetime(2021, 11, i+1)
                                                    for i in range(7)]),
                          'classes': [5, 6, 8, 2, 3, 7, 4]})
 
# Plotting the time series of given dataframe
plt.plot(dataframe.date_of_week, dataframe.classes)
 
# Giving title to the chart using plt.title
plt.title('Classes by Date')
 
# rotating the x-axis tick labels at 30degree
# towards right
plt.xticks(rotation=30, ha='right')
 
# Providing x and y label to the chart
plt.xlabel('Date')
plt.ylabel('Classes')

Output:

We can also create scatter plots with the help of Time Series with the help of matplotlib.

Example 2: Scatter time series plot of the given dataframe




#import modules
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
 
# Let say we have a dataframe of the days of
# the week and number of classes in each day of the upcoming week.
# Taking 7 days from 1-11-2021 to 7-11-2021
 
dataframe = pd.DataFrame({'date_of_week': np.array([datetime.datetime(2021, 11, i+1)
                                                    for i in range(7)]),
                          'classes': [5, 6, 8, 2, 3, 7, 4]})
 
# To draw scatter time series plot of the given dataframe
plt.plot_date(dataframe.date_of_week, dataframe.classes)
 
# rotating the x-axis tick labels at 30degree towards right
plt.xticks(rotation=30, ha='right')
 
# Giving title to the chart using plt.title
plt.title('Classes by Date')
 
# Providing x and y label to the chart
plt.xlabel('Date')
plt.ylabel('Classes')

Output:

Similarly, we can plot the time series of two dataFrames and compare them. Let say we have two colleges -‘XYZ’ and ‘ABC’. Now we need to compare these two by time-series graph of matplotlib. 

Example 3:




# Initialising required libraries
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
 
# ABC colllege classes by date- from 01-11-2021 to 07-11-2021
abc = pd.DataFrame({'date_of_week': np.array([datetime.datetime(2021, 11, i+1)
                                              for i in range(7)]),
                    'classes': [5, 6, 8, 2, 3, 7, 4]})
 
 
# XYZ colllege classes by date - from 01-11-2021 to 07-11-2021
xyz = pd.DataFrame({'date_of_week': np.array([datetime.datetime(2021, 11, i+1)
                                              for i in range(7)]),
                    'classes': [2, 3, 7, 3, 4, 1, 2]})
 
# plotting the time series of ABC college dataframe
plt.plot(abc.date_of_week, abc.classes)
 
# plotting the time series of XYZ college dataframe
plt.plot(xyz.date_of_week, xyz.classes, color='green')
 
# Giving title to the graph
plt.title('Classes by Date')
 
# rotating the x-axis tick labels at 30degree
# towards right
plt.xticks(rotation=30, ha='right')
 
# Giving x and y label to the graph
plt.xlabel('Date')
plt.ylabel('Classes')

Output:

Similarly, we can plot the time series plot from a dataset. Here is the link to the dataset

Example 4:




# Initialising required libraries
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
 
# Loading the dataset
data = pd.read_csv("C:/Users/aparn/Desktop/data.csv")
 
# X axis is price_date
price_date = data['Date']
 
# Y axis is price closing
price_close = data['Close']
 
# Plotting the timeseries graph of given dataset
plt.plot(price_date, price_close)
 
# Giving title to the graph
plt.title('Prices by Date')
 
# rotating the x-axis tick labels at 30degree
# towards right
plt.xticks(rotation=30, ha='right')
 
# Giving x and y label to the graph
plt.xlabel('Price Date')
plt.ylabel('Price Close')

Output:


Article Tags :