Skip to content

ENH: LogLocator can auto-select number of ticks based on axis #7126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4418,9 +4418,19 @@ def test_adjust_numtick_aspect():
@image_comparison(baseline_images=["auto_numticks"], style='default',
extensions=['png'])
def test_auto_numticks():
# Make tiny, empty subplots, verify that there are only 3 ticks.
fig, axes = plt.subplots(4, 4)


@image_comparison(baseline_images=["auto_numticks_log"], style='default',
extensions=['png'])
def test_auto_numticks_log():
# Verify that there are not too many ticks with a large log range.
fig, ax = plt.subplots()
matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
ax.loglog([1e-20, 1e5], [1e-16, 10])


@cleanup
def test_broken_barh_empty():
fig, ax = plt.subplots()
Expand Down
41 changes: 29 additions & 12 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,11 @@ def set_params(self, **kwargs):

def _raw_ticks(self, vmin, vmax):
if self._nbins == 'auto':
nbins = max(min(self.axis.get_tick_space(), 9),
max(1, self._min_n_ticks - 1))
if self.axis is not None:
nbins = max(min(self.axis.get_tick_space(), 9),
max(1, self._min_n_ticks - 1))
else:
nbins = 9
else:
nbins = self._nbins

Expand Down Expand Up @@ -1757,15 +1760,19 @@ class LogLocator(Locator):
Determine the tick locations for log axes
"""

def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=15):
def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
"""
place ticks on the location= base**i*subs[j]
"""
if numticks is None:
if rcParams['_internal.classic_mode']:
numticks = 15
else:
numticks = 'auto'
self.base(base)
self.subs(subs)
# this needs to be validated > 1 with traitlets
self.numticks = numticks
self.numdecs = numdecs
self.numticks = numticks

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

def subs(self, subs):
"""
set the minor ticks the log scaling every base**i*subs[j]
set the minor ticks for the log scaling every base**i*subs[j]
"""
if subs is None:
self._subs = None # autosub
Expand All @@ -1799,6 +1806,14 @@ def __call__(self):
return self.tick_values(vmin, vmax)

def tick_values(self, vmin, vmax):
if self.numticks == 'auto':
if self.axis is not None:
numticks = max(min(self.axis.get_tick_space(), 9), 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the minimum of 2, but 9 seems a bit of a random number. Why was this chosen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it is pretty random. It is the default max we chose for the MaxNLocator auto algorithm, so I used it here as well. The rationale in both cases is that plots just don't look good with a huge number of major ticks; so 9 is random, but it "seems reasonable" as a limit. Nothing prevents a user from specifying a larger number instead of using the auto algorithm.

else:
numticks = 9
else:
numticks = self.numticks

b = self._base
# dummy axis has no axes attribute
if hasattr(self.axis, 'axes') and self.axis.axes.name == 'polar':
Expand Down Expand Up @@ -1836,12 +1851,14 @@ def tick_values(self, vmin, vmax):
subs = self._subs

stride = 1
if not self.numticks > 1:
raise RuntimeError('The number of ticks must be greater than 1 '
'for LogLocator.')
# FIXME: The following was designed for integer division in py2.
while numdec / stride + 1 > self.numticks:
stride += 1

if rcParams['_internal.classic_mode']:
# Leave the bug left over from the PY2-PY3 transition.
while numdec / stride + 1 > numticks:
stride += 1
else:
while numdec // stride + 1 > numticks:
stride += 1

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