Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6741ddf

Browse files
authored
Merge pull request #7126 from efiring/logauto
ENH: LogLocator can auto-select number of ticks based on axis
2 parents fd7eeeb + 7a84962 commit 6741ddf

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,9 +4418,19 @@ def test_adjust_numtick_aspect():
44184418
@image_comparison(baseline_images=["auto_numticks"], style='default',
44194419
extensions=['png'])
44204420
def test_auto_numticks():
4421+
# Make tiny, empty subplots, verify that there are only 3 ticks.
44214422
fig, axes = plt.subplots(4, 4)
44224423

44234424

4425+
@image_comparison(baseline_images=["auto_numticks_log"], style='default',
4426+
extensions=['png'])
4427+
def test_auto_numticks_log():
4428+
# Verify that there are not too many ticks with a large log range.
4429+
fig, ax = plt.subplots()
4430+
matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
4431+
ax.loglog([1e-20, 1e5], [1e-16, 10])
4432+
4433+
44244434
@cleanup
44254435
def test_broken_barh_empty():
44264436
fig, ax = plt.subplots()

lib/matplotlib/ticker.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,8 +1639,11 @@ def set_params(self, **kwargs):
16391639

16401640
def _raw_ticks(self, vmin, vmax):
16411641
if self._nbins == 'auto':
1642-
nbins = max(min(self.axis.get_tick_space(), 9),
1643-
max(1, self._min_n_ticks - 1))
1642+
if self.axis is not None:
1643+
nbins = max(min(self.axis.get_tick_space(), 9),
1644+
max(1, self._min_n_ticks - 1))
1645+
else:
1646+
nbins = 9
16441647
else:
16451648
nbins = self._nbins
16461649

@@ -1757,15 +1760,19 @@ class LogLocator(Locator):
17571760
Determine the tick locations for log axes
17581761
"""
17591762

1760-
def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=15):
1763+
def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
17611764
"""
17621765
place ticks on the location= base**i*subs[j]
17631766
"""
1767+
if numticks is None:
1768+
if rcParams['_internal.classic_mode']:
1769+
numticks = 15
1770+
else:
1771+
numticks = 'auto'
17641772
self.base(base)
17651773
self.subs(subs)
1766-
# this needs to be validated > 1 with traitlets
1767-
self.numticks = numticks
17681774
self.numdecs = numdecs
1775+
self.numticks = numticks
17691776

17701777
def set_params(self, base=None, subs=None, numdecs=None, numticks=None):
17711778
"""Set parameters within this locator."""
@@ -1786,7 +1793,7 @@ def base(self, base):
17861793

17871794
def subs(self, subs):
17881795
"""
1789-
set the minor ticks the log scaling every base**i*subs[j]
1796+
set the minor ticks for the log scaling every base**i*subs[j]
17901797
"""
17911798
if subs is None:
17921799
self._subs = None # autosub
@@ -1799,6 +1806,14 @@ def __call__(self):
17991806
return self.tick_values(vmin, vmax)
18001807

18011808
def tick_values(self, vmin, vmax):
1809+
if self.numticks == 'auto':
1810+
if self.axis is not None:
1811+
numticks = max(min(self.axis.get_tick_space(), 9), 2)
1812+
else:
1813+
numticks = 9
1814+
else:
1815+
numticks = self.numticks
1816+
18021817
b = self._base
18031818
# dummy axis has no axes attribute
18041819
if hasattr(self.axis, 'axes') and self.axis.axes.name == 'polar':
@@ -1836,12 +1851,14 @@ def tick_values(self, vmin, vmax):
18361851
subs = self._subs
18371852

18381853
stride = 1
1839-
if not self.numticks > 1:
1840-
raise RuntimeError('The number of ticks must be greater than 1 '
1841-
'for LogLocator.')
1842-
# FIXME: The following was designed for integer division in py2.
1843-
while numdec / stride + 1 > self.numticks:
1844-
stride += 1
1854+
1855+
if rcParams['_internal.classic_mode']:
1856+
# Leave the bug left over from the PY2-PY3 transition.
1857+
while numdec / stride + 1 > numticks:
1858+
stride += 1
1859+
else:
1860+
while numdec // stride + 1 > numticks:
1861+
stride += 1
18451862

18461863
decades = np.arange(math.floor(vmin) - stride,
18471864
math.ceil(vmax) + 2 * stride, stride)

0 commit comments

Comments
 (0)