Crash when using zoom tools on a plot: AutoMinorLocator after MultipleLocator gives "ValueError: Need at least two major ticks to find minor tick locations" · Issue #807 · matplotlib/matplotlib · GitHub
Crash when using zoom tools on a plot: AutoMinorLocator after MultipleLocator gives "ValueError: Need at least two major ticks to find minor tick locations" #807
Run the following code and then zoom in with the zoom-box tool or the cross and right-click drag to zoom in.
from pylab import *
from matplotlib.ticker import MultipleLocator, AutoMinorLocator
plot([0,3],[0,2.2])
ax = gca()
ax.xaxis.set_major_locator(MultipleLocator(0.5))
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
show()
Once MultipleLocator has been called, the auto-reassigning of tick spacing when zooming (either with the zoom box or the cross and right-click drag) does not happen, and then AutoMinorLocator gives the following error because it has "majorstep = majorlocs[1] - majorlocs[0]" and majorlocs has less than 2 elements when zoomed in that far.
"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 )"
Ok, I took a look at the source of the error, class AutoMinorLocator(Locator), in ticker.py, and came up with a very simple fix that involves editing 1 line and adding 2 lines.
Inside
classAutoMinorLocator(Locator):
change the following
exceptIndexError:
raiseValueError('Need at least two major ticks to find minor tick locations')
Then when there are less than 2 major ticks visible on an axis, the minor ticks are not shown at all. (When zooming back out, the minor ticks are shown again.) This seems to be the best solution, since there does not seem to be an axis property to get tick locations that are outside the plot view range.
How does one go about submitting this to the matplotlib package?
Activity
cgra commented on Mar 29, 2012
Found with matplotlib 1.1.0, and confirmed by Mike Kaufman on Mar 28 2012 github checkout (see matplotlib-users mailing list, thread view of http://sourceforge.net/mailarchive/message.php?msg_id=29040134).
cgra commented on Jun 21, 2012
Ok, I took a look at the source of the error, class AutoMinorLocator(Locator), in ticker.py, and came up with a very simple fix that involves editing 1 line and adding 2 lines.
Inside
change the following
to
and change the following
to
The second change could also be done with a try/except instead of an if/else, e.g.
Then when there are less than 2 major ticks visible on an axis, the minor ticks are not shown at all. (When zooming back out, the minor ticks are shown again.) This seems to be the best solution, since there does not seem to be an axis property to get tick locations that are outside the plot view range.
How does one go about submitting this to the matplotlib package?
Implements @cgra (Christopher Graves) suggestion for issue matplotlib…