Plotting with Dates
Matplotlib is a Python library for creating charts and visualizations — and it understands real calendar dates, so a list of datetime objects becomes a properly spaced, neatly labelled time axis.
Learn Plotting with Dates in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll build date objects, plot them on the x-axis, format the tick labels with matplotlib.dates, and fix overlapping labels with autofmt_xdate().
What You'll Learn in This Lesson
1 Plotting a List of Dates
Build your dates with Python's datetime module, then pass that list as the x values. Matplotlib recognizes the date objects and lays them out on a proper time axis automatically.
What you'll see: a line rising from January to April, with the four dates correctly spaced along the bottom. Matplotlib spaces them by actual time, not just by position in the list.
2 Formatting Date Labels with mdates
To control how the dates are written, import matplotlib.dates as mdates and attach a DateFormatter to the x-axis. The format string uses the same codes as Python's strftime — for example '%b %Y' renders "Jan 2024".
What you'll see: the same trend line, but now the x-axis ticks read "Jan 2024", "Feb 2024", and so on instead of raw numbers — clear, human-readable month labels.
3 Fixing Overlap with autofmt_xdate()
When you have many dates, the labels collide and become unreadable. One call — fig.autofmt_xdate() — rotates and right-aligns the date labels so they fit neatly along the axis.
What you'll see: a full-year curve with all twelve month labels tilted at an angle along the bottom, neatly right-aligned so none of them overlap — even though there are many of them.
🎯 Your Turn: Fill in the Blanks
Replace each ___ to plot dates and format them as "Jan 2025".
📋 Common Errors & Quick Reference
You passed date strings , not date objects. Build them with date(2024, 1, 1) from the datetime module.
Call fig.autofmt_xdate() to rotate them, or use a coarser DateFormatter.
Call
What It Does
from datetime import date
Get the date type
import matplotlib.dates as mdates
Date tick tools
mdates.DateFormatter('%b %Y')
Label format (e.g. Jan 2024)
fig.autofmt_xdate()
Rotate crowded date labels
🏆 Mini Challenge: A Week of Temperatures
Plot seven daily temperatures, label the axis as "Jun 10" style days, and rotate them.
❓ Frequently Asked Questions
Lesson 14 complete — you can plot time itself!
You built date objects, plotted them on a time axis, formatted the ticks with DateFormatter, and tamed crowded labels with autofmt_xdate().
🚀 Up next: Twin Axes & Secondary Y — show two different scales on one chart.
Practice quiz
Can Matplotlib plot Python date objects directly on the x-axis?
- Yes — it recognizes them and spaces them by time
- No, you must convert to strings
- Only with pandas
- Only integers work
Answer: Yes — it recognizes them and spaces them by time. Pass a list of date/datetime objects and Matplotlib lays them out on a time axis.
What is the standard import alias for the date-tools module?
- import matplotlib.dates as dt
- import matplotlib.dates as mdates
- import mdates
- from matplotlib import dates as d
Answer: import matplotlib.dates as mdates. The conventional alias is mdates: import matplotlib.dates as mdates.
Which class formats date tick labels?
- mdates.DateLocator
- mdates.AutoDate
- mdates.DateFormatter
- mdates.TimeAxis
Answer: mdates.DateFormatter. mdates.DateFormatter('%b %Y') controls how dates are written.
Which call attaches a formatter to the x-axis?
- ax.format_xaxis()
- ax.set_format()
- plt.dateformat()
- ax.xaxis.set_major_formatter(...)
Answer: ax.xaxis.set_major_formatter(...). ax.xaxis.set_major_formatter(mdates.DateFormatter(...)) applies the format.
What does the format code %b produce?
- The short month name (e.g. Jan)
- The full year
- The day number
- The weekday
Answer: The short month name (e.g. Jan). %b is the abbreviated month name, the same strftime code Python uses.
What does %Y produce in a DateFormatter string?
- The two-digit year
- The four-digit year
- The month
- The hour
Answer: The four-digit year. %Y is the four-digit year, e.g. 2024.
Which call rotates crowded date labels so they don't overlap?
- ax.rotate()
- plt.tilt()
- fig.autofmt_xdate()
- ax.spread_labels()
Answer: fig.autofmt_xdate(). fig.autofmt_xdate() rotates and right-aligns the date labels.
Why might dates show as plain numbers instead of dates?
- The figure is too small
- autofmt was called
- viridis was used
- You passed date strings instead of date objects
Answer: You passed date strings instead of date objects. Build real date objects with date(2024, 1, 1), not strings.
Which import gives you the date type?
- from datetime import date
- import dates
- from time import date
- import calendar
Answer: from datetime import date. from datetime import date provides the date class.
What does the format string '%b %Y' render for January 2024?
- 2024-01
- Jan 2024
- 01/2024
- January
Answer: Jan 2024. %b %Y produces the short month and four-digit year, e.g. 'Jan 2024'.
Continue this course
- Previous: Annotations & Text
- Next: Twin Axes & Secondary Y