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.
ax.xaxis.set_major_locator(
    matplotlib.dates.WeekdayLocator(byweekday=matplotlib.dates.MO)
)
ax.xaxis.set_major_formatter(
    matplotlib.dates.DateFormatter('%a %d\n%b %Y')
)

and now I have this plot:

nicely formatted date ticks

Nice.

11 January, 2010 - 23:19

Thanks So much for share this

bruststraffung (not verified)

Thanks So much for share this information, i was needing this code to add my locator, because im not than skilful to realice by myself.
bruststraffung

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options