
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to show date and time on the X-axis in Matplotlib?
To show date and time on the X-axis in Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a list of dates and y values.
- Get the current axis.
- Set the major date formatter and locator.
- Plot x and y values using plot() method.
- To display the figure, use show() method.
Example
from datetime import datetime as dt from matplotlib import pyplot as plt, dates as mdates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True dates = ["01/02/2020", "01/03/2020", "01/04/2020"] x_values = [dt.strptime(d, "%m/%d/%Y").date() for d in dates] y_values = [1, 2, 3] ax = plt.gca() formatter = mdates.DateFormatter("%Y-%m-%d") ax.xaxis.set_major_formatter(formatter) locator = mdates.DayLocator() ax.xaxis.set_major_locator(locator) plt.plot(x_values, y_values) plt.show()
Output
- Related Articles
- How to force Matplotlib to show the values on X-axis as integers?
- Show the origin axis (x,y) in Matplotlib plot
- Creating a graph with date and time in axis labels with Matplotlib
- How to customize the X-axis in Matplotlib?
- Moving X-axis in Matplotlib during real-time plot
- How to change the range of the X-axis and Y-axis in Matplotlib?
- Show decimal places and scientific notation on the axis of a Matplotlib plot
- How to plot data against specific dates on the X-axis using Matplotlib?
- How to customize X-axis ticks in Matplotlib?
- How to set "step" on axis X in my figure in Matplotlib Python 2.6.6?
- How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?
- How to set X-axis values in Matplotlib Python?
- How to add a second X-axis in Matplotlib?
- Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?
- How to annotate a range of the X-axis in Matplotlib?

Advertisements