How to Plot a Time Series in Matplotlib?
Last Updated :
27 Jan, 2022
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
- X variable belongs to the datetime. datetime() class in the given dataframe.
- Y variable belongs to the values corresponding to date
We can also rotate the axis by using xticks() function
Syntax:
plt.xticks(rotation, ha)
where
- rotation describes the degrees you want to rotate
- ha describes the position like right, left, top, bottom
Approach
- We need to have two axes for our graph i.e X and Y-axis. We will start by having a dataframe to plot the graph.
- We can either make our own data frame or use some publicly available data frames. In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time.
- plt.plot() method is used to plot the graph in matplotlib.
- To provide labels and title to make our graph meaningful we can use methods like – plt.title(), plt.xlabel(), plt.ylabel()
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
Python3
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
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 ]})
plt.plot(dataframe.date_of_week, dataframe.classes)
plt.title( 'Classes by Date' )
plt.xticks(rotation = 30 , ha = 'right' )
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
Python3
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
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 ]})
plt.plot_date(dataframe.date_of_week, dataframe.classes)
plt.xticks(rotation = 30 , ha = 'right' )
plt.title( 'Classes by Date' )
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:
Python3
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
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 = 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 ]})
plt.plot(abc.date_of_week, abc.classes)
plt.plot(xyz.date_of_week, xyz.classes, color = 'green' )
plt.title( 'Classes by Date' )
plt.xticks(rotation = 30 , ha = 'right' )
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:
Python3
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
data = pd.read_csv( "C:/Users/aparn/Desktop/data.csv" )
price_date = data[ 'Date' ]
price_close = data[ 'Close' ]
plt.plot(price_date, price_close)
plt.title( 'Prices by Date' )
plt.xticks(rotation = 30 , ha = 'right' )
plt.xlabel( 'Price Date' )
plt.ylabel( 'Price Close' )
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...