Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass Python datetime.datetime values or NumPy datetime64 values directly to Matplotlib: for an ordinary date plot, no manual conversion is needed. Matplotlib converts the dates to numeric coordinates and selects date-aware tick locators and formatters. The main decisions are how many ticks to show, how to format them, which timezone to display, and whether the date representation has enough precision for your timestamps.

Plot timestamps directly

Matplotlib’s built-in units converter handles Python datetime sequences and NumPy datetime64 arrays, adding date-appropriate tick behavior to the axis. See the Matplotlib guides to plotting dates and strings and the matplotlib.dates API.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(times, values)
ax.set_xlabel("Time")
ax.set_ylabel("Value")
plt.show()

Here, times contains date-like values and values contains the corresponding y-values. Keep them aligned: each timestamp should describe the value at the same position. You generally do not need to convert the dates to numbers yourself.

Choose date tick locations and labels

Automatic date ticks are a good starting point. When the default spacing or labels do not suit the chart, use locators to choose tick positions and formatters to choose their text. The appropriate interval depends on the displayed range: seconds for a short event, days for a few weeks, or months or years for longer spans.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set a predictable day interval

This example places ticks on the 1st and 15th of each month and labels them with abbreviated month names and day numbers:

import matplotlib.dates as mdates

ax.xaxis.set_major_locator(mdates.DayLocator(bymonthday=[1, 15]))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))

Use a locator suited to the intended cadence, such as MonthLocator or DayLocator. For an adaptive choice, AutoDateLocator and AutoDateFormatter select date ticks and formats based on the range and scale. ConciseDateFormatter can reduce repetition when nearby labels share a year or month. The available date tools are documented in Matplotlib’s date API.

Prevent crowded labels

If labels overlap, first reduce the number of ticks by changing the locator interval. You can also rotate the tick labels; Matplotlib’s date-tick text guide demonstrates rotation and date formatting. Rotation helps fit labels, but it does not replace choosing a sensible tick density.

Control the timezone shown on the axis

Matplotlib’s date conversion, locators, and formatters are timezone-aware. The default timezone is rcParams['timezone'], which is UTC by default. If the chart must show another zone, specify that timezone in the relevant date conversion or tick-formatting tools rather than assuming the display will follow the machine’s local timezone. The date API documentation describes the timezone options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Understand timestamp precision and the date epoch

Matplotlib represents dates as floating-point numbers of days relative to an epoch. The default epoch is 1970-01-01 UTC. Because floating-point resolution depends on distance from that origin, precision decreases for dates farther away from it.

  • For dates approximately within 70 years on either side of the default epoch, Matplotlib documents achievable microsecond precision.
  • Elsewhere in its supported date range, years 0001–9999, the documentation describes precision of approximately 20 microseconds.
  • If a plot needs sub-microsecond resolution, the Matplotlib documentation recommends plotting floating-point seconds instead of datetime-like values.

These are properties of the date-coordinate representation, not guarantees that the input data itself is accurate to those intervals. See Matplotlib’s guide to date precision and epochs.

When fine precision is needed far from 1970

If datetime-like values must retain microsecond precision for dates far from the default epoch, set a closer epoch before any date conversion takes place. If your use case needs still finer resolution, compare that approach with plotting floating-point seconds. Choose the representation based on the time range and resolution the data actually requires; a visually detailed axis cannot restore precision lost in the coordinates.

A practical configuration checklist

  • Input: Use Python datetime or NumPy datetime64 values and plot them directly.
  • Range: Match the tick interval to the span being shown, from short intervals to months or years.
  • Labels: Choose a formatter that gives enough context without repeating information unnecessarily.
  • Timezone: Set the intended display zone explicitly when it matters; otherwise the documented default is UTC.
  • Precision: Check distance from the epoch for microsecond work, and use floating-point seconds for sub-microsecond plots.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.