- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 do I change the range of the X-axis with datetimes in Matplotlib?
To change the range of the X-axis with datetimes in matplotlib, we can take the following steps −
Create a list of x and y, where x stores the datetime and y stores the number.
Using subplots method, create a figure and add a set of subplots.
Plot x and y data points using plots() method, wehere markerface color is green, marker edge color is red, and marker size is 7.
Since date ticklabels often overlap, so it is useful to rorate them and right-align them using autofmt_xdate() method.
To change the range of X-axis with datetimes, use set_xlim() with range of datetimes.
To change the range of Y-axis, use set_ylim() method.
To display the figure, use show() method.
Example
import datetime import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [datetime.date(2021, 1, 1), datetime.date(2021, 1, 3), datetime.date(2021, 1, 5), datetime.date(2021, 1, 7)] y = [1, 3, 5, 7] fig, ax = plt.subplots() ax.plot_date(x, y, markerfacecolor='green', markeredgecolor='red', ms=7) fig.autofmt_xdate() ax.set_xlim([datetime.date(2020, 12, 31), datetime.date(2021, 1, 8)]) ax.set_ylim([0, 8]) plt.show()
Output
- Related Articles
- How to change the range of the X-axis and Y-axis in Matplotlib?
- How to annotate a range of the X-axis in Matplotlib?
- How do I change matplotlib's subplot projection of an existing axis?
- How do I change the axis tick font in a Matplotlib plot when rendering using LaTeX?
- How do I format the axis number format to thousands with a comma in Matplotlib?
- How do I change the font size of the scale in Matplotlib plots?
- How to enforce axis range in Matplotlib?
- How to customize the X-axis in Matplotlib?
- How do I convert (or scale) axis values and redefine the tick frequency in Matplotlib?
- Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?
- How to set the range of Y-axis for a Seaborn boxplot using Matplotlib?
- How to change the position of X-axis in base R plot?
- Plotting dates on the X-axis with Python's Matplotlib
- How to shift a graph along the X-axis in matplotlib?
- How to set my xlabel at the end of X-axis in Matplotlib?

Advertisements