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

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Mar 31 00:05:56 CEST 2006


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

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

    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)
  ax.plot(nx.mlab.rand(1000))
  ax.xaxis.set_major_locator(MaxNLocator(4))
  show()


JDH





More information about the Python-list mailing list