Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Proposed change to default log scale tick formatting #5161

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 9 commits into from
Aug 22, 2016
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests
add cleanup for test
  • Loading branch information
zblz committed Aug 2, 2016
commit d1a5aff5a3676925b1eb81f7b18e58c8dd34f7a4
72 changes: 72 additions & 0 deletions 72 lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def test_AutoMinorLocator():
assert_almost_equal(ax.xaxis.get_ticklocs(minor=True), test_value)


def _sub_labels(axis, subs=()):
"Test whether locator marks subs to be labeled"
loc = axis.get_minor_locator()
minor_tlocs = axis.get_minorticklocs()
coefs = minor_tlocs / 10**(np.floor(np.log10(minor_tlocs)))
label = [np.round(c) in subs for c in coefs]
assert_equal(loc.show_tick_label(minor_tlocs), label)

@cleanup
def test_LogLocator():
loc = mticker.LogLocator(numticks=5)
assert_raises(ValueError, loc.tick_values, 0, 1000)
Expand All @@ -63,6 +72,31 @@ def test_LogLocator():
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
assert_almost_equal(loc.tick_values(1, 100), test_value)

# test label locator
Copy link
Member

Choose a reason for hiding this comment

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

This should have a @cleanup decorator to make sure that the plot is closed correctly after the test

fig, ax = plt.subplots()
ax.set_xscale('log')
ax.xaxis.set_major_locator(mticker.LogLocator(base=10, subs=[]))
ax.xaxis.set_minor_locator(mticker.LogLocator(base=10,
subs=np.arange(2, 10)))
# axis range above 3 decades, only bases are labeled
ax.set_xlim(1, 1e4)
loc = ax.xaxis.get_major_locator()
show_major_labels = loc.show_tick_label(ax.xaxis.get_majorticklocs())
assert np.all(show_major_labels)
_sub_labels(ax.xaxis, subs=[])

# axis range at 2 to 3 decades, label sub 3
ax.set_xlim(1, 800)
_sub_labels(ax.xaxis, subs=[3])

# axis range at 1 to 2 decades, label subs 2 and 5
ax.set_xlim(1, 80)
_sub_labels(ax.xaxis, subs=[2, 5])

# axis range at 0 to 1 decades, label subs 2, 3, 6
ax.set_xlim(1, 8)
_sub_labels(ax.xaxis, subs=[2, 3, 6])


def test_LinearLocator_set_params():
"""
Expand Down Expand Up @@ -252,6 +286,44 @@ def get_view_interval(self):
yield _logfe_helper, formatter, base, locs, i, expected_result


def test_LogFormatterSciNotation():
test_cases = {
10: (
(1e-05, '${10^{-5}}$'),
(1, '${10^{0}}$'),
(100000, '${10^{5}}$'),
(2e-05, '${2\\times10^{-5}}$'),
(2, '${2\\times10^{0}}$'),
(200000, '${2\\times10^{5}}$'),
(3.1415926535897935e-05, '${3.14159\\times10^{-5}}$'),
(3.141592653589793, '${3.14159\\times10^{0}}$'),
(314159.2653589793, '${3.14159\\times10^{5}}$'),
(5e-05, '${5\\times10^{-5}}$'),
(5, '${5\\times10^{0}}$'),
(500000, '${5\\times10^{5}}$'),
(8.5e-05, '${8.5\\times10^{-5}}$'),
(8.5, '${8.5\\times10^{0}}$'),
(850000.0, '${8.5\\times10^{5}}$'),
),
2: (
(0.03125, '${2^{-5}}$'),
(1, '${2^{0}}$'),
(32, '${2^{5}}$'),
(0.0375, '${1.2\\times2^{-5}}$'),
(1.2, '${1.2\\times2^{0}}$'),
(38.4, '${1.2\\times2^{5}}$'),
(0.044194173824159223, '${1.41421\\times2^{-5}}$'),
(1.4142135623730951, '${1.41421\\times2^{0}}$'),
(45.254833995939045, '${1.41421\\times2^{5}}$')),
}

for base in test_cases.keys():
formatter = mticker.LogFormatterSciNotation(base=base)
for value, expected in test_cases[base]:
with matplotlib.rc_context({'text.usetex': False}):
nose.tools.assert_equal(formatter(value), expected)


def _pprint_helper(value, domain, expected):
fmt = mticker.LogFormatter()
label = fmt.pprint_val(value, domain)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.