Matplotlib.axes.Axes.plot_date() in Python (original) (raw)
Last Updated : 13 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
matplotlib.axes.Axes.plot_date() Function
The Axes.plot_date() function in axes module of matplotlib library is used to plot data that contains dates.
Syntax: Axes.plot_date(self, x, y, fmt=’o’, tz=None, xdate=True, ydate=False, *, 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.axes.Axes.plot_date() function in matplotlib.axes:
Example-1:
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
,
6
)
delta
=
datetime.timedelta(hours
=
24
)
dates
=
drange(date1, date2, delta)
y
=
np.arange(
len
(dates))
fig, ax
=
plt.subplots()
ax.plot_date(dates, y
*
*
2
)
ax.set_title(
'matplotlib.axes.Axes.plot_date Example1'
)
plt.show()
Output:
Example-2:
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
)
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()
ax.set_title(
'matplotlib.axes.Axes.plot_date Example2'
)
plt.show()
Output: