Matplotlib.pyplot.plot_date() function in Python - GeeksforGeeks

05 Jan, 2022
Matplotlib is a module or package or library in python which is used for data visualization. Pyplot is an interface to a Matplotlib module that provides a MATLAB-like interface. 
This function  used to add dates to the plot.
Syntax:
matplotlib.pyplot.plot_date(x, y, fmt=’o’, tz=None, xdate=True, ydate=False,  data=None, **kwargs)
This is the syntax of date function. It contains various parameters or arguments which are explained below.
1.
x, y
x and y both are the coordinates of the data i.e. x-axis horizontally and y-axis vertically.
2.
fmt
It is a optional string parameter that contains the corresponding plot details like color, style etc. 
3.
tz
tz stands for timezone used to label dates, default(UTC).
4.
xdate
xdate parameter contains boolean value. If xdate is true then x-axis is interpreted as date in matplotlib. By default xdate is true.
5.
ydate
If ydate is true then y-axis is interpreted as date in matplotlib. By default ydate is false.
6.
data
The data which is going to be used in plot.
The last parameter **kwargs is the Keyword arguments control the Line2D properties like animation, dash_ joint-style, colors, linewidth, linestyle, marker, etc.
Example 1:
Python3
# importing librariesimport matplotlib.pyplot as pltfrom datetime import datetime # creating array of dates for x axisdates = [    datetime(2020, 6, 30),    datetime(2020, 7, 22),    datetime(2020, 8, 3),    datetime(2020, 9, 14)] # for y axisx = [0, 1, 2, 3] plt.plot_date(dates, x, 'g')plt.xticks(rotation=70)plt.show()
Output:
Example 2: Creating a plot using dataset.
Python3
# importing librariesimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetime # creating a dataframedata = pd.DataFrame({'Date': [datetime(2020, 6, 30),                              datetime(2020, 7, 22),                              datetime(2020, 8, 3),                              datetime(2020, 9, 14)],                                           'Close': [8800, 2600, 8500, 7400]}) # x-axisprice_date = data['Date'] # y-axisprice_close = data['Close'] plt.plot_date(price_date, price_close, linestyle='--', color='r')plt.title('Market', fontweight="bold")plt.xlabel('Date of Closing')plt.ylabel('Closing Amount') plt.show()
Example 3: Changing the format of the date:
Python3
# importing librariesimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetime # creating a dataframedata = pd.DataFrame({'Date': [datetime(2020, 6, 30),                              datetime(2020, 7, 22),                              datetime(2020, 8, 3),                              datetime(2020, 9, 14)],                                           'Close': [8800, 2600, 8500, 7400]}) # x-axisprice_date = data['Date'] # y-axisprice_close = data['Close'] plt.plot_date(price_date, price_close, linestyle='--', color='r')plt.title('Market', fontweight="bold")plt.xlabel('Date of Closing')plt.ylabel('Closing Amount') # Changing the format of the date using# dateformatter classformat_date = mpl_dates.DateFormatter('%d-%m-%Y') # getting the accurate current axes using gca()plt.gca().xaxis.set_major_formatter(format_date) plt.show()
Output:
The format of the date changed to dd-mm-yyyy. To know more about dataformatter and gca() click here.
clintra
Matplotlib Pyplot-class
Picked
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Defaultdict in Python
Python OOPs Concepts
Python | os.path.join() method
Python | Pandas dataframe.groupby()