Note

Go to the endto download the full example code.

Placing date ticks using recurrence rules#

TheiCalender RFC specifiesrecurrence rules (rrules), that definedate sequences. You can use rrules in Matplotlib to place date ticks.

This example sets custom date ticks on every 5th easter.

Seehttps://dateutil.readthedocs.io/en/stable/rrule.html for help with rrules.

date demo rrule
importdatetimeimportmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.datesimportYEARLY,DateFormatter,RRuleLocator,drange,rrulewrapper# Fixing random state for reproducibilitynp.random.seed(19680801)# tick every 5th easterrule=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 valuesfig,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()

Gallery generated by Sphinx-Gallery