Skip to content

FIX: make sure we have more than 1 tick with small log ranges #18754

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
Oct 21, 2020
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
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,3 +1362,13 @@ def test_bad_locator_subs(sub):
ll = mticker.LogLocator()
with pytest.raises(ValueError):
ll.subs(sub)


@pytest.mark.parametrize('numticks', [1, 2, 3, 9])
@pytest.mark.style('default')
def test_small_range_loglocator(numticks):
ll = mticker.LogLocator()
ll.set_params(numticks=numticks)
for top in [5, 7, 9, 11, 15, 50, 100, 1000]:
ticks = ll.tick_values(.5, top)
assert (np.diff(np.log10(ll.tick_values(6, 150))) == 1).all()
Copy link
Contributor

Choose a reason for hiding this comment

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

@tacaswell There seems to be a clear typo in the test here: you don't do anything with ticks, and the assert is the same on each loop iteration. Do you remember what you intended to do?

7 changes: 7 additions & 0 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,13 @@ def tick_values(self, vmin, vmax):
if mpl.rcParams['_internal.classic_mode'] else
(numdec + 1) // numticks + 1)

# if we have decided that the stride is as big or bigger than
Copy link
Member

Choose a reason for hiding this comment

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

Is this really the right solution to the original problem? To me, it seems that numticks is 1 because there isn't room for more ticks. Do we really want ticks that overwrite each other for very narrow axes?

Copy link
Member

@timhoffm timhoffm Oct 17, 2020

Choose a reason for hiding this comment

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

If you are very narrow, there's no good solution. Either you have only one tick, which makes your scale uninterpretable. Or you have overlap. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, let me rephrase: We clip numticks to 2, so why don't we have 2 ticks?

Copy link
Member Author

Choose a reason for hiding this comment

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

numticks is an upper bound and only "best effort" at that.

The issue is that based on numtick and a rounded "in" version of the range we pick a stride between major ticks (which is meant to catch things like 30 orders of magnitude with 9 ticks). If we propose 3 ticks, have a range of [5, 150], then we "round out" to [1, 100] for the range giving us numdec = 2 and numticks = 3 and a stride of (3 / 3) + 1 == 2 which means we put ticks at (10 ^ {-2, 0, 2, 4}) which in turn means that only the tick at 100 is actually shown. Additionally because we only show the minor ticks if the major stride is equal to 1 you end up with exactly 1 tick and 1 label shown even though the users / auto logic asked for up to 3.

I think that this fix (which fixes the values after doing a bunch of heuristics) is a better and more reliable fix than making the heuristics more complicated.

# the range, clip the stride back to the available range - 1
# with a floor of 1. This prevents getting axis with only 1 tick
# visible.
if stride >= numdec:
stride = max(1, numdec - 1)

# Does subs include anything other than 1? Essentially a hack to know
# whether we're a major or a minor locator.
have_subs = len(subs) > 1 or (len(subs) == 1 and subs[0] != 1.0)
Expand Down