I was working on a time-series visualization project in Python, where I needed to plot daily sales data for a retail store in the USA. The challenge I faced was controlling how the dates appeared on the X-axis in my Matplotlib charts.
If you’ve ever plotted dates using plot_date() in Matplotlib, you might have noticed that the date labels sometimes overlap, appear too dense, or don’t display in the format you want. This can make your chart look cluttered and hard to read.
In this tutorial, I’ll show you two simple ways to control dates on the X-axis and customize XTicks in a Matplotlib plot created using the plot_date() function.
Dates in Python Matplotlib
When you plot dates in Matplotlib, it internally converts them into floating-point numbers representing days since an epoch (default: January 1, 1970). The library then uses date locators and formatters to display readable labels on the X-axis.
This means that while you can directly pass datetime objects to Matplotlib, you still need to tell it how to show and space out the ticks on the X-axis.
Let’s now explore the two main methods to control date formatting and XTicks in Matplotlib.
Step-by-Step Example Using DateFormatter
Below is a complete working Python example that shows how to format dates on the X-axis.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
# Generate sample date data
dates = [datetime(2025, 10, i) for i in range(1, 11)]
sales = [150, 200, 250, 220, 300, 280, 350, 400, 370, 420]
# Create a date plot
plt.figure(figsize=(10, 5))
plt.plot_date(dates, sales, linestyle='solid', marker='o', color='teal')
# Format date on X-axis
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d, %Y'))
# Rotate date labels for better readability
plt.gcf().autofmt_xdate()
# Add labels and title
plt.title('Daily Sales Report - October 2025')
plt.xlabel('Date')
plt.ylabel('Sales (in USD)')
plt.grid(True)
plt.show()You can see the output in the screenshot below.

In this code, I used DateFormatter(‘%b %d, %Y’) to display the date in the format “Oct 01, 2025”. You can adjust the format to show only months (‘%b’), or even hours and minutes if you’re plotting time-series data.
Another approach I often use is the AutoDateLocator function. This automatically determines the best date intervals based on your dataset size, ensuring your chart looks clean and readable.
Example using AutoDateLocator
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
# Generate 30 days of data
dates = [datetime(2025, 9, 1) + timedelta(days=i) for i in range(30)]
visits = [100 + i * 5 for i in range(30)]
# Plot the data
plt.figure(figsize=(10, 5))
plt.plot_date(dates, visits, linestyle='solid', color='darkorange')
# Use AutoDateLocator for dynamic date spacing
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
plt.gca().xaxis.set_major_locator(locator)
plt.gca().xaxis.set_major_formatter(formatter)
# Add labels and title
plt.title('Website Visits - September 2025')
plt.xlabel('Date')
plt.ylabel('Number of Visits')
plt.grid(True)
plt.show()You can see the output in the screenshot below.

The AutoDateLocator automatically adjusts the tick marks based on your data range. Combined with ConciseDateFormatter, it keeps your date labels clean and minimal, ideal for dashboards or reports.
Method 1: Control XTicks in Matplotlib using set_xticks()
Now that we’ve controlled how the dates appear on the X-axis, let’s move on to controlling the tick positions themselves.
Sometimes, you might want to show ticks only for specific dates, for example, every Monday or the first of each month. This can be done easily using the set_xticks() method.
Example using set_xticks()
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
# Create date data for two weeks
dates = [datetime(2025, 10, i) for i in range(1, 15)]
revenue = [120, 150, 130, 160, 180, 200, 210, 230, 250, 270, 260, 280, 300, 320]
plt.figure(figsize=(10, 5))
plt.plot_date(dates, revenue, linestyle='solid', color='purple')
# Show ticks only for every 3rd day
tick_dates = dates[::3]
plt.gca().set_xticks(tick_dates)
# Format the date labels
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
plt.title('Revenue Growth - October 2025')
plt.xlabel('Date')
plt.ylabel('Revenue (USD)')
plt.grid(True)
plt.gcf().autofmt_xdate()
plt.show()You can see the output in the screenshot below.

Here, I manually selected every third date using slicing (dates[::3]) and passed it to set_xticks().
Method 2: Control XTicks in Matplotlib using DayLocator and MonthLocator
For more flexibility, you can use date locators like DayLocator, WeekdayLocator, or MonthLocator. These let you define exactly which intervals you want your ticks to appear on.
Example using DayLocator and MonthLocator
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
# Generate 90 days of data
dates = [datetime(2025, 7, 1) + timedelta(days=i) for i in range(90)]
profits = [500 + (i * 10) for i in range(90)]
plt.figure(figsize=(12, 6))
plt.plot_date(dates, profits, linestyle='solid', color='green')
# Set major ticks for each month and minor ticks for every 7 days
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.gca().xaxis.set_minor_locator(mdates.WeekdayLocator(interval=1))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
plt.title('Quarterly Profit Trend - Q3 2025')
plt.xlabel('Date')
plt.ylabel('Profit (USD)')
plt.grid(True)
plt.gcf().autofmt_xdate()
plt.show()You can see the output in the screenshot below.

With this approach, you can create professional-looking charts that display months as major ticks and weekly intervals as minor ticks.
I often use this method when preparing quarterly or annual reports for clients who want clear time-based breakdowns.
Additional Tips for Working with Dates in Matplotlib
Here are a few extra tips that I’ve learned over the years while working with Python and Matplotlib:
- Use plt.tight_layout() to prevent labels from overlapping.
- If your chart looks cluttered, try plt.xticks(rotation=45) for better readability.
- Use ConciseDateFormatter when you want minimal but informative date labels.
- Always format your dates before plotting if you’re reading them from a CSV file using pandas.to_datetime().
These small tweaks can make a big difference in how professional your charts look.
Working with dates in Matplotlib can seem tricky at first, but once you understand how to control the X-axis and XTicks, it becomes easy.
- Two for controlling how dates appear on the X-axis (DateFormatter and AutoDateLocator).
- Two for customizing XTicks (set_xticks() and DayLocator/MonthLocator).
Each method gives you a different level of control, from simple formatting to precise tick placement. I hope you found this article helpful. Try these methods in your next Python data visualization project, and your charts will look cleaner, more readable, and more professional.
You may also read:
- Log‑Log Scale in Matplotlib with Minor Ticks and Colorbar
- Matplotlib plot_date for Scatter and Multiple Line Charts
- Change Linestyle and Color in Matplotlib plot_date() Plots
- Date Format and Convert Dates in Matplotlib plot_date

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.