使用递归规则放置日期刻度#

iCalender RFC 指定了递归规则 (rrules),它们定义了日期序列。你可以在 Matplotlib 中使用 rrules 来放置日期刻度。

此示例在每个 5 月复活节设置自定义日期刻度。

有关 rrules 的帮助,请参阅 https://dateutil.readthedocs.io/en/stable/rrule.html

date demo rrule
import datetime

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.dates import (YEARLY, DateFormatter, RRuleLocator, drange,
                              rrulewrapper)

# Fixing random state for reproducibility
np.random.seed(19680801)


# tick every 5th easter
rule = rrulewrapper(YEARLY, byeaster=1, interval=5)
loc = RRuleLocator(rule)
formatter = DateFormatter('%m/%d/%y')
date1 = datetime.date(1952, 1, 1)
date2 = datetime.date(2004, 4, 12)
delta = datetime.timedelta(days=100)

dates = drange(date1, date2, delta)
s = np.random.rand(len(dates))  # make up some random y values


fig, ax = plt.subplots()
plt.plot(dates, s, 'o')
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)

plt.show()

由 Sphinx-Gallery 生成的图库