473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 18861
>>>>> "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Hunter | last post by:
matplotlib is a 2D plotting package for python with a matlab compatible syntax and output tested under linux and windows platforms. matplotlib-0.30 is available for download at...
8
by: Derek Basch | last post by:
Can anyone give any suggestions on how to make a logarithmic (base 10) x and y axis (loglog) plot in matplotlib? The scatter function doesn't seem to have any log functionality built into it. ...
5
by: Enigma Curry | last post by:
I'm playing around with matplotlib for the first time. I'm trying to make a very simple histogram of values 1-6 and how many times they occur in a sequence. However, after about an hour of...
2
by: Grant Edwards | last post by:
I downloaded examples/contour_demo.py, and it doesn't run. I've searched both the user guide and the Wiki for "contour" and got zero hits. ...
5
by: John Henry | last post by:
I've been asking this question at the matplotlib user list and never gotten an answer. I am hoping that there are matplotlib users here that can help. My problem with matplotlib's way of...
4
by: Bill Jackson | last post by:
Hi, I'm having some trouble plotting with the following matplotlibrc: text.usetex : True I tried clearing the cache files under ~/.matplotlib, but this did not help the problem. I'd post...
0
by: Dick Crepeau | last post by:
I would like to control the y axis of a plot. The following code does exactly what I want it to! On my linux computer it sets the y axis limits to 18.0 minimum, 58.0 maximum, plots some points on...
1
by: Eric Holbrook | last post by:
I'm using matplotlib to generate (and save) plots of bandwidth data from simulation logs. Since the simulations are of varying lengths, i need a way to scale the axes so that every 100,000 points...
2
by: Durand | last post by:
Hi, I'm trying to plot a simple graph against date or time using matplotlib. I've read about date_plot but I'm not really sure how to use it. At the moment, I have some data arranged into lists,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.