I don't know if this is helpful or not, but I've seen some complaints in the archives about a problem that I've been facing, so I thought I'd post my solution.

I am auto-generating graphs of weekly data. In terms of pixels the graphs will always be the same size, but the number of weeks included can vary from a single week to two year's worth of data. For many of these graphs matplotlib does a fine job of placing ticks in a helpful and readable manner. But for some situations where I have say, six weeks of data, it places ticks every few days in such a way that they overlap and are unreadable.

The MaxNLocator is helpful in this situation as I can use it to limit the number of ticks, but it isn't ideal because I really only want the first day of the week labeled, not some arbitrary location in the middle of the weeks. So I can up with the following locator that will place a limited number of labels at places along the axis that make sense. You pass it the max number of bins and the width of the data. So for my needs I give it 6 bins and a width of 7 for weekly data. If there are fewer dates then it only labels the start of each week, not days in between. It there are more weeks than that it tries to group them into 6 or fewer bins. I've done some testing and it works well for me.

class MaxNDateLocator(Locator):

def __init__(self, num_bins, item_width):

self.bins = num_bins

self.width = item_width

def __call__(self):

self.verify_intervals()

vmin, vmax = self.dataInterval.get_bounds()

delta = vmax - vmin

mul = 1

if (mul*self.bins*self.width < delta):

mul = int(delta/(self.bins*self.width)) + 1

return range(vmin,vmax+1,mul*self.width)