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.
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.’
In the earlier section, We have seen the syntax for plot_date(). Now let’s implement it.
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
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)
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.
In this step, We will finally invoke the plot_date() function. After it, We will use plt.show() for the graph presentation.
plt.plot_date(date_obj, data_points_YAxis)
plt.show()
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.
Thanks
Data Science Learner Team