Date tick control in pylab/matplotlib

18 May, 2009 - 14:05

Today I struggled a bit with pylab's plot_date function and overlapping date tick labels:

overlapping plot_date xticks

After some googling, I found that pylab offers nice flexible date tick control and formatting. I added a date tick locator and a date formatter to the mix as follows:

import pylab
import matplotlib
import matplotlib.dates
fig = pylab.figure()
ax = fig.gca()
# Plotting stuff here ...
ax.plot_date(dates, values, 'b.-')
# Set major x ticks on Mondays.
    matplotlib.dates.DateFormatter('%a %d\n%b %Y')

and now I have this plot:

nicely formatted date ticks