- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to plot data against specific dates on the X-axis using Matplotlib?
To plot data against specific dates on the X-axis using matplotlib, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Make a list of dates and convert them in datetime format as x.
Make a list of y data points.
Set the formatter of the major ticker.
Set the locator of the major ticker.
Plot x and y data points using plot() method.
To display the figure, use show() method.
Example
from datetime import datetime 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/2021", "01/03/2021", "01/04/2021", "01/05/2021", "01/06/2021", ] x = [datetime.strptime(d, "%m/%d/%Y").date() for d in dates] y = [1, 5, 3, 8, 4] ax = plt.gca() ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d")) ax.xaxis.set_major_locator(mdates.DayLocator()) ax.plot(x, y) plt.show()
Output
- Related Articles
- Plotting dates on the X-axis with Python's Matplotlib
- How to create a plot in base R with dates sequence on X-axis?
- How to plot a rectangle on a datetime axis using Matplotlib?
- Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?
- How to plot int to datetime on X-axis using Seaborn?
- How to plot a kernel density plot of dates in Pandas using Matplotlib?
- Adding caption below X-axis for a scatter plot using Matplotlib
- Show the origin axis (x,y) in Matplotlib plot
- How to plot arbitrary markers on a Pandas data series using Matplotlib?
- How to add footnote under the X-axis using Matplotlib?
- How to plot events on time using Matplotlib?
- Plot scatter points on polar axis in Matplotlib
- How to plot the X-axis labels on the upper-side of the plot in R?
- Moving X-axis in Matplotlib during real-time plot
- How to remove or hide X-axis labels from a Seaborn / Matplotlib plot?

Advertisements