Re: [Matplotlib-users] how to maintain fractional minor tick spacing From: Christopher Graves - 2012-03-11 13:33:03 Attachments: Message as HTML ```On Sun, Mar 11, 2012 at 2:06 PM, Mike Kaufman 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 ```