How to Implement Matplotlib plot_date in Python ?

How to Implement Matplotlib plot_date in Python

Matplotlib plot_date function is useful in implementing date datatype on charts. We implement it in time series and forecasting visualizations. In this article we you see,  how quickly! we can draw charts with dates in the axis.

Matplotlib plot_date Syntax-

It has very clear syntax. Firstly we will see this function with required parameters only.

matplotlib.pyplot.plot_date(x, y)


Where the required parameters are –

x -It must be a list of date objects.

Y -Data Points on Y-Axis.

This function plot_date() supports multiple optional parameters also which are-

1. xdate=True, ydate=False are default set to put dates object in X-Axis. In case we need to reverse it. I mean if we need the date objects on the y-axis we will have to change their default value accordingly.

2. fmt and tz are also optional parameters. Where tz is for timezone.’


Matplotlib plot_date Implementation Stepwise-

In the earlier section, We have seen the syntax for plot_date(). Now let’s implement it.

Step 1:  Import Packages –

In this step, We will only import the packages. Actually, in demonstrating plot_date()function we need the following packages.

import matplotlib.pyplot as plt 
from matplotlib.dates import drange 
import numpy as np 
import datetime 

Step 2: Date Object Initialization-

In this section, We will only focus on creating date objects. Here is the code for that.

start_date = datetime.datetime(1990, 3, 1) 
end_date = datetime.datetime(1990, 3, 5) 
time_delta = datetime.timedelta(hours = 24) 
date_obj = drange(start_date, end_date, time_delta)  

Step 3: Y-Axis Data Point Creation-

In real scenarios, We must have real data. But here just for the sake of demonstration, We will create some data points for Y-Axis.

data_points_YAxis = np.arange(len(date_obj)) 

It will create a NumPy array with equal length of data points as date_obj.


Step 4: Calling plot_date()  –

In this step, We will finally invoke the plot_date() function. After it, We will use for the graph presentation.

plt.plot_date(date_obj, data_points_YAxis) 
matplotlib plot_date implementation
matplotlib plot_date implementation

Conclusion –

plot_date() function is helpful in plotting charts with dates in Matplotlib. Especially in Time series and Sales Forecasting etc, Where date is must to have the attribute. I hope this article must give you a complete idea about plot_date() function. Still, if you need some more information, please comment below.


Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner