Categories
Python Answers

How to edit the date formatting of x-axis tick labels in matplotlib with Python?

Spread the love

Sometimes, we want to edit the date formatting of x-axis tick labels in matplotlib with Python.

In this article, we’ll look at how to edit the date formatting of x-axis tick labels in matplotlib with Python.

How to edit the date formatting of x-axis tick labels in matplotlib with Python?

To edit the date formatting of x-axis tick labels in matplotlib with Python, we can call the set_major_formatter method.

For instance, we write

import matplotlib.dates as mdates

myFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFmt)

to create a DateFormatter object that returns the 2 digit date from a date.

Then we call ax.xaxis.set_major_formatter with myFmt to set the formatter for the x-axis to myFmt to format dates to 2 digit dates.

Conclusion

To edit the date formatting of x-axis tick labels in matplotlib with Python, we can call the set_major_formatter method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *