Skip to content

Commit 7e47149

Browse files
committed
Merge pull request matplotlib#961 from WeatherGod/issue-807-AutoMinorLocator
Issue 807 auto minor locator
2 parents 0f8d32b + a6a9624 commit 7e47149

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/matplotlib/ticker.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -1527,15 +1527,22 @@ def __call__(self):
15271527
try:
15281528
majorstep = majorlocs[1] - majorlocs[0]
15291529
except IndexError:
1530-
raise ValueError('Need at least two major ticks to find minor '
1531-
'tick locations')
1530+
# Need at least two major ticks to find minor tick locations
1531+
# TODO: Figure out a way to still be able to display minor
1532+
# ticks without two major ticks visible. For now, just display
1533+
# no ticks at all.
1534+
majorstep = 0
15321535

15331536
if self.ndivs is None:
1534-
x = int(round(10 ** (np.log10(majorstep) % 1)))
1535-
if x in [1, 5, 10]:
1536-
ndivs = 5
1537-
else:
1538-
ndivs = 4
1537+
if majorstep == 0 :
1538+
# TODO: Need a better way to figure out ndivs
1539+
ndivs = 1
1540+
else :
1541+
x = int(round(10 ** (np.log10(majorstep) % 1)))
1542+
if x in [1, 5, 10]:
1543+
ndivs = 5
1544+
else:
1545+
ndivs = 4
15391546
else:
15401547
ndivs = self.ndivs
15411548

@@ -1545,12 +1552,15 @@ def __call__(self):
15451552
if vmin > vmax:
15461553
vmin,vmax = vmax,vmin
15471554

1548-
t0 = majorlocs[0]
1549-
tmin = np.ceil((vmin - t0) / minorstep) * minorstep
1550-
tmax = np.floor((vmax - t0) / minorstep) * minorstep
1551-
locs = np.arange(tmin, tmax, minorstep) + t0
1552-
cond = np.abs((locs - t0) % majorstep) > minorstep/10.0
1553-
locs = locs.compress(cond)
1555+
if len(majorlocs) > 0:
1556+
t0 = majorlocs[0]
1557+
tmin = np.ceil((vmin - t0) / minorstep) * minorstep
1558+
tmax = np.floor((vmax - t0) / minorstep) * minorstep
1559+
locs = np.arange(tmin, tmax, minorstep) + t0
1560+
cond = np.abs((locs - t0) % majorstep) > minorstep/10.0
1561+
locs = locs.compress(cond)
1562+
else:
1563+
locs = []
15541564

15551565
return self.raise_if_exceeds(np.array(locs))
15561566

0 commit comments

Comments
 (0)