Matplotlib cftime Axis Support

1.4.1 · active · verified Thu Apr 16

nc-time-axis (current version 1.4.1) provides seamless integration for plotting `cftime.datetime` objects on `matplotlib` axes. It handles various calendar types and automatically formats tick labels. The library is actively maintained with releases focusing on compatibility with `matplotlib` and `cftime` updates, as well as bug fixes and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to plot `cftime.datetime` objects on a Matplotlib axis using `nc-time-axis`. Simply importing `nc_time_axis` registers the necessary converters. You can optionally use `CFTimeFormatter` for custom tick label formatting.

import cftime
import matplotlib.pyplot as plt
import nc_time_axis # This import registers the converters
import numpy as np

# Create some sample cftime data
x_values = np.linspace(0, 6 * np.pi, 100)
y_values = 0.5 * x_values + np.sin(x_values)
times = cftime.num2date(x_values, units="days since 2000-01-01", calendar="noleap")

# Plotting with cftime.datetime objects on the x-axis
fig, ax = plt.subplots(1, 1)
ax.plot(times, y_values)

ax.set_xlabel("Time")
ax.set_ylabel("Value")
ax.set_title("Plot with cftime.datetime Axis")

# For explicit tick formatting, use CFTimeFormatter
# formatter = nc_time_axis.CFTimeFormatter("%Y-%m-%d", "noleap")
# ax.xaxis.set_major_formatter(formatter)

plt.tight_layout()
plt.show()

view raw JSON →