On Sun, Mar 11, 2012 at 2:32 PM, Christopher Graves <christoph.graves@gmail.com> wrote:
On Sun, Mar 11, 2012 at 2:06 PM, Mike Kaufman <mckauf@gmail.com> wrote:
On 3/11/12 8:14 AM, cgraves wrote:

Hi, here is an example script which places minor ticks with 2 per major tick
(minor tick spacing is "fractional" of major tick spacing with relative
interval of 1/2):

from pylab import *
fig=figure()
ax=subplot(111)
ax.autoscale(tight=True)
plot([1,2,4],[1,2,3])
x_ticks_maj_spacing =
float(abs(ax.xaxis.get_ticklocs()[0]-ax.xaxis.get_ticklocs()[1]))
x_ticks_min_spacing = x_ticks_maj_spacing/2
ax.xaxis.set_minor_locator(MultipleLocator(x_ticks_min_spacing))
y_ticks_maj_spacing =
float(abs(ax.yaxis.get_ticklocs()[0]-ax.yaxis.get_ticklocs()[1]))
y_ticks_min_spacing = y_ticks_maj_spacing/2
ax.yaxis.set_minor_locator(MultipleLocator(y_ticks_min_spacing))
show()

This works fine. However, if one changes the axes limits then the major
ticks get automatically adjusted to a different interval but the minor ticks
remain at the positions they were already at. To see this, either use the
zoom tools or do the following after running the above:

xlim(1,2.5)
fig.canvas.draw()

The question is, what is the best way to maintain the fractional minor tick
spacing? I suppose one could set up a way to update the set_minor_locator
and redraw the figure each time the figure axes limits are adjusted, but is
there a better way?

Try this:

from pylab import *
from matplotlib.ticker import AutoMinorLocator

clf()
ax=subplot(111)
ax.autoscale(tight=True)
plot([1,2,4],[1,2,3])
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
draw()

M

PS: I believe this is a fairly new feature...

Thanks! Great news that AutoMinorLocator has been added and accomplishes this. Regarding the P.S. I can confirm that the feature was not in matplotlib 1.0.1 - I had to update to 1.1.0 to use it.

Best /Chris


Hi Mike,

A follow-up question... When using that, if one then tries to manually use the zoom-box tool available with a matplotlib plot, if one draws too small of a box (less than 2 major ticks in x or y dimension, based on the following error message), it gives the following error and further operations on the plot do not work.

ValueError: Need at least two major ticks to find minor tick locations
( File "/usr/lib/pymodules/python2.7/matplotlib/ticker.py", line 1528, in __call__ )

Any way to avoid this for now? (And ultimately, should this be made into a bug fix request?)

Best /Chris