Skip to content

Commit 99ab593

Browse files
committed
Fix matplotlib#5488: Adjust nticks based on length of axis
1 parent b698502 commit 99ab593

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎lib/matplotlib/axis.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,13 @@ def axis_date(self, tz=None):
16651665
tz = pytz.timezone(tz)
16661666
self.update_units(datetime.datetime(2009, 1, 1, 0, 0, 0, 0, tz))
16671667

1668+
def get_tick_space(self):
1669+
"""
1670+
Return the estimated number of ticks that can fit on the axis.
1671+
"""
1672+
# Must be overridden in the subclass
1673+
raise NotImplementedError()
1674+
16681675

16691676
class XAxis(Axis):
16701677
__name__ = 'xaxis'
@@ -1988,6 +1995,18 @@ def set_default_intervals(self):
19881995
self.axes.viewLim.intervalx = xmin, xmax
19891996
self.stale = True
19901997

1998+
def get_tick_space(self):
1999+
if self._tick_space is None:
2000+
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
2001+
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
2002+
tick = self._get_tick(True)
2003+
# There is a heuristic here that the aspect ratio of tick text
2004+
# is no more than 2:1
2005+
size = tick.label1.get_size() * 3
2006+
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2007+
self._tick_space = np.floor(length / size)
2008+
return self._tick_space
2009+
19912010

19922011
class YAxis(Axis):
19932012
__name__ = 'yaxis'
@@ -2318,3 +2337,13 @@ def set_default_intervals(self):
23182337
if not viewMutated:
23192338
self.axes.viewLim.intervaly = ymin, ymax
23202339
self.stale = True
2340+
2341+
def get_tick_space(self):
2342+
# TODO: cache this computation
2343+
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2344+
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72.0
2345+
tick = self._get_tick(True)
2346+
# Having a spacing of at least 2 just looks good.
2347+
size = tick.label1.get_size() * 2.0
2348+
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2349+
return np.floor(length / size)

‎lib/matplotlib/ticker.py‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,48 @@ def view_limits(self, dmin, dmax):
14621462
return np.take(self.bin_boundaries(dmin, dmax), [0, -1])
14631463

14641464

1465+
class AutoSpacedLocator(MaxNLocator):
1466+
"""
1467+
Behaves like a MaxNLocator, except N is automatically determined
1468+
from the length of the axis.
1469+
"""
1470+
def __init__(self, *args, **kwargs):
1471+
"""
1472+
Keyword args:
1473+
1474+
*steps*
1475+
Sequence of nice numbers starting with 1 and ending with 10;
1476+
e.g., [1, 2, 4, 5, 10]
1477+
1478+
*integer*
1479+
If True, ticks will take only integer values.
1480+
1481+
*symmetric*
1482+
If True, autoscaling will result in a range symmetric
1483+
about zero.
1484+
1485+
*prune*
1486+
['lower' | 'upper' | 'both' | None]
1487+
Remove edge ticks -- useful for stacked or ganged plots
1488+
where the upper tick of one axes overlaps with the lower
1489+
tick of the axes above it.
1490+
If prune=='lower', the smallest tick will
1491+
be removed. If prune=='upper', the largest tick will be
1492+
removed. If prune=='both', the largest and smallest ticks
1493+
will be removed. If prune==None, no ticks will be removed.
1494+
1495+
"""
1496+
if 'nbins' in kwargs:
1497+
raise ValueError(
1498+
'AutoSpacedLocator does not take nbins as an argument')
1499+
self.set_params(**self.default_params)
1500+
self.set_params(**kwargs)
1501+
1502+
def __call__(self):
1503+
self._nbins = self.axis.get_tick_space()
1504+
return super(AutoSpacedLocator, self).__call__()
1505+
1506+
14651507
def decade_down(x, base=10):
14661508
'floor x to the nearest lower decade'
14671509
if x == 0.0:
@@ -1885,9 +1927,9 @@ def tick_values(self, vmin, vmax):
18851927
return self.raise_if_exceeds(np.array(ticklocs))
18861928

18871929

1888-
class AutoLocator(MaxNLocator):
1930+
class AutoLocator(AutoSpacedLocator):
18891931
def __init__(self):
1890-
MaxNLocator.__init__(self, nbins=9, steps=[1, 2, 5, 10])
1932+
AutoSpacedLocator.__init__(self, steps=[1, 2, 5, 10])
18911933

18921934

18931935
class AutoMinorLocator(Locator):

0 commit comments

Comments
 (0)