Thursday, October 23, 2025
HomeLanguagesmatplotlib.pyplot.plot_date() in Python

matplotlib.pyplot.plot_date() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.plot_date() Function:

The plot_date() function in pyplot module of matplotlib library is used to plot with data that contains dates.

Syntax:

matplotlib.pyplot.plot_date(x, y, fmt='o', tz=None, xdate=True, ydate=False, hold=None, data=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x, y: These parameter are the horizontal and vertical coordinates of the data points.
  • fmt: This parameter is an optional parameter and it contains the string value.
  • tz: This parameter is the time zone to use in labeling dates.It contain timezone string.
  • xdate: This parameter is also an optional parameter. And it contain boolean values with default value True. If True, the x-axis will be interpreted as Matplotlib dates.
  • ydate: This parameter is also an optional parameter. And it contain boolean values with default value True. If True, the y-axis will be interpreted as Matplotlib dates.

Returns: This returns the following:

  • lines:This returns the list of Line2D objects representing the plotted data.

Below examples illustrate the matplotlib.pyplot.plot_date() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib function
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import drange
import numpy as np
   
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 12)
delta = datetime.timedelta(hours = 24)
dates = drange(date1, date2, delta)
   
y = np.arange(len(dates))
   
plt.plot_date(dates, y ** 2)
plt.title('matplotlib.pyplot.plot_date() function Example', fontweight ="bold")
plt.show()


Output:

Example #2:




# Implementation of matplotlib function
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
   
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
   
y = np.arange(len(dates))
   
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2, 'g')
   
ax.set_xlim(dates[0], dates[-1])
   
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('% Y-% m-% d'))
   
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
plt.title('matplotlib.pyplot.plot_date() function Example', fontweight ="bold")
plt.show()


Output:

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS