-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve tick subsampling in LogLocator. #29698
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
+167
−63
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Improved selection of log-scale ticks | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The algorithm for selecting log-scale ticks (on powers of ten) has been | ||
improved. In particular, it will now always draw as many ticks as possible | ||
(e.g., it will not draw a single tick if it was possible to fit two ticks); if | ||
subsampling ticks, it will prefer putting ticks on integer multiples of the | ||
subsampling stride (e.g., it prefers putting ticks at 10\ :sup:`0`, 10\ :sup:`3`, | ||
10\ :sup:`6` rather than 10\ :sup:`1`, 10\ :sup:`4`, 10\ :sup:`7`) if this | ||
results in the same number of ticks at the end; and it is now more robust | ||
against floating-point calculation errors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,13 +332,11 @@ def test_basic(self): | |
with pytest.raises(ValueError): | ||
loc.tick_values(0, 1000) | ||
|
||
test_value = np.array([1.00000000e-05, 1.00000000e-03, 1.00000000e-01, | ||
1.00000000e+01, 1.00000000e+03, 1.00000000e+05, | ||
1.00000000e+07, 1.000000000e+09]) | ||
test_value = np.array([1e-5, 1e-3, 1e-1, 1e+1, 1e+3, 1e+5, 1e+7]) | ||
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value) | ||
|
||
loc = mticker.LogLocator(base=2) | ||
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.]) | ||
test_value = np.array([.5, 1., 2., 4., 8., 16., 32., 64., 128.]) | ||
assert_almost_equal(loc.tick_values(1, 100), test_value) | ||
|
||
def test_polar_axes(self): | ||
|
@@ -377,7 +375,7 @@ def test_tick_values_correct(self): | |
1.e+01, 2.e+01, 5.e+01, 1.e+02, 2.e+02, 5.e+02, | ||
1.e+03, 2.e+03, 5.e+03, 1.e+04, 2.e+04, 5.e+04, | ||
1.e+05, 2.e+05, 5.e+05, 1.e+06, 2.e+06, 5.e+06, | ||
1.e+07, 2.e+07, 5.e+07, 1.e+08, 2.e+08, 5.e+08]) | ||
1.e+07, 2.e+07, 5.e+07]) | ||
assert_almost_equal(ll.tick_values(1, 1e7), test_value) | ||
|
||
def test_tick_values_not_empty(self): | ||
|
@@ -387,8 +385,7 @@ def test_tick_values_not_empty(self): | |
1.e+01, 2.e+01, 5.e+01, 1.e+02, 2.e+02, 5.e+02, | ||
1.e+03, 2.e+03, 5.e+03, 1.e+04, 2.e+04, 5.e+04, | ||
1.e+05, 2.e+05, 5.e+05, 1.e+06, 2.e+06, 5.e+06, | ||
1.e+07, 2.e+07, 5.e+07, 1.e+08, 2.e+08, 5.e+08, | ||
1.e+09, 2.e+09, 5.e+09]) | ||
1.e+07, 2.e+07, 5.e+07, 1.e+08, 2.e+08, 5.e+08]) | ||
assert_almost_equal(ll.tick_values(1, 1e8), test_value) | ||
|
||
def test_multiple_shared_axes(self): | ||
|
@@ -1913,14 +1910,54 @@ def test_bad_locator_subs(sub): | |
ll.set_params(subs=sub) | ||
|
||
|
||
@pytest.mark.parametrize('numticks', [1, 2, 3, 9]) | ||
@pytest.mark.parametrize("numticks, lims, ticks", [ | ||
(1, (.5, 5), [.1, 1, 10]), | ||
(2, (.5, 5), [.1, 1, 10]), | ||
(3, (.5, 5), [.1, 1, 10]), | ||
(9, (.5, 5), [.1, 1, 10]), | ||
(1, (.5, 50), [.1, 10, 1_000]), | ||
(2, (.5, 50), [.1, 1, 10, 100]), | ||
(3, (.5, 50), [.1, 1, 10, 100]), | ||
(9, (.5, 50), [.1, 1, 10, 100]), | ||
(1, (.5, 500), [.1, 10, 1_000]), | ||
(2, (.5, 500), [.01, 1, 100, 10_000]), | ||
(3, (.5, 500), [.1, 1, 10, 100, 1_000]), | ||
(9, (.5, 500), [.1, 1, 10, 100, 1_000]), | ||
(1, (.5, 5000), [.1, 100, 100_000]), | ||
(2, (.5, 5000), [.001, 1, 1_000, 1_000_000]), | ||
(3, (.5, 5000), [.001, 1, 1_000, 1_000_000]), | ||
(9, (.5, 5000), [.1, 1, 10, 100, 1_000, 10_000]), | ||
]) | ||
@mpl.style.context('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() | ||
def test_small_range_loglocator(numticks, lims, ticks): | ||
ll = mticker.LogLocator(numticks=numticks) | ||
assert_array_equal(ll.tick_values(*lims), ticks) | ||
|
||
|
||
@mpl.style.context('default') | ||
def test_loglocator_properties(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not super obvious to me reading the code what this is testing - would be good to get a comment explaining at the top here. |
||
# Test that LogLocator returns ticks satisfying basic desirable properties | ||
# for a wide range of inputs. | ||
max_numticks = 8 | ||
pow_end = 20 | ||
for numticks, (lo, hi) in itertools.product( | ||
range(1, max_numticks + 1), itertools.combinations(range(pow_end), 2)): | ||
ll = mticker.LogLocator(numticks=numticks) | ||
decades = np.log10(ll.tick_values(10**lo, 10**hi)).round().astype(int) | ||
# There are no more ticks than the requested number, plus exactly one | ||
# tick below and one tick above the limits. | ||
assert len(decades) <= numticks + 2 | ||
assert decades[0] < lo <= decades[1] | ||
assert decades[-2] <= hi < decades[-1] | ||
stride, = {*np.diff(decades)} # Extract the (constant) stride. | ||
# Either the ticks are on integer multiples of the stride... | ||
if not (decades % stride == 0).all(): | ||
# ... or (for this given stride) no offset would be acceptable, | ||
# i.e. they would either result in fewer ticks than the selected | ||
# solution, or more than the requested number of ticks. | ||
for offset in range(0, stride): | ||
alt_decades = range(lo + offset, hi + 1, stride) | ||
assert len(alt_decades) < len(decades) or len(alt_decades) > numticks | ||
|
||
|
||
def test_NullFormatter(): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.