how to maintain fractional minor tick spacing

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?

Best,
Chris

···

--
View this message in context: http://old.nabble.com/how-to-maintain-fractional-minor-tick-spacing-tp33480612p33480612.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

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...

···

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?

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

···

On Sun, Mar 11, 2012 at 2:06 PM, Mike Kaufman <mckauf@…287…> 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…

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

···

On Sun, Mar 11, 2012 at 2:32 PM, Christopher Graves <christoph.graves@…287…> wrote:

On Sun, Mar 11, 2012 at 2:06 PM, Mike Kaufman <mckauf@…287…> 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

Ok, I seem to remember seeing this error before, but I can't trip it now (with either 1.1.1rc or today's git checkout of 1.2.x). Do you have
a short script that can reproduce this? For me, the zoom-box tool seems to be [correctly] setting the majortick locations as I zoom in, thus preventing this exception. I should note that I'm using the GTKAgg frontend. This may be the issue. A long time ago I was using the MacOSX frontend, and maybe this was when I was seeing it...

Aside from that, this would be a bug.

M

···

On 3/26/12 12:49 PM, Christopher Graves wrote:

On Sun, Mar 11, 2012 at 2:32 PM, Christopher Graves > <christoph.graves@...287... <mailto:christoph.graves@…287…>> wrote:

        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?)