Matplotlib: How to set number of ticks on an axis?

John Hunter jdhunter at
Fri Mar 31 00:05:56 CEST 2006

>>>>> "Caleb" == Caleb Hattingh <caleb.hattingh at> writes:

    Caleb> It seems that the locater() classes are where I should
    Caleb> look, and there seem to be some defaults in

    Caleb> class AutoLocator(MaxNLocator): def __init__(self):
    Caleb> MaxNLocator.__init__(self, nbins=9, steps=[1, 2, 5, 10])

    Caleb> I don't understand what this means :)

    Caleb> I would prefer not to hack this directly in the matplotlib
    Caleb> code.  How can I change the number of ticks on an axis
    Caleb> programmatically without messing with the other ticklabel
    Caleb> functionality?

Yes, you will want to use a MaxNLocator.  Note that the MaxNLocator
sets the maximum number of *intervals* so the max number of ticks will
be the max number of intervals plus one.  

  from matplotlib.ticker import MaxNLocator
  from pylab import figure, show, nx

  fig = figure()
  ax = fig.add_subplot(111)


More information about the Python-list mailing list