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.
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 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.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.