470,647 Members | 1,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

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

Hi

I tried several Google searches to no avail. I read through pretty
much most of the online docs at the matplotlib sourceforge site, but
didn't find what I was looking for. I went through the axis.py and
ticker.py code today, trying to find out how to set the number of
points (ticks) on an axis in Matplotlib.

I know that something like
xticks(arange(5))


will make the x-axis have the values specified, but Matplotlib appears
to have a very nice way of setting axis ticks with human-friendly
values that round in just the right way for a given set of data. I
still want that functionality, but I want to set how many ticks are on
a given axis.

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

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

I don't understand what this means :)

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

Caleb

Mar 30 '06 #1
2 18684
>>>>> "Caleb" == Caleb Hattingh <ca************@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
Mar 30 '06 #2
John,

Thank you.

Caleb

Mar 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by John Hunter | last post: by
8 posts views Thread by Derek Basch | last post: by
2 posts views Thread by Grant Edwards | last post: by
5 posts views Thread by John Henry | last post: by
4 posts views Thread by Bill Jackson | last post: by
reply views Thread by Dick Crepeau | last post: by
1 post views Thread by Eric Holbrook | last post: by
2 posts views Thread by Durand | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.