Skip to content

Change LogFormatterExponent to consistently format negative exponents #2691

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 4 commits into from
Jan 13, 2014
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
32 changes: 32 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ def test_LogLocator():
assert_almost_equal(loc.tick_values(1, 100), test_value)

Copy link
Member

Choose a reason for hiding this comment

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

missing blank line here


def test_LogFormatterExponent():
class FakeAxis(object):
"""Allow Formatter to be called without having a "full" plot set up."""
def get_view_interval(self):
return 1, 10

i = np.arange(-3, 4, dtype=float)
expected_result = ['-3', '-2', '-1', '0', '1', '2', '3']
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to test small negative (and positive values) also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pelson - Absolutely, thanks! I added more non-integer tests with multiple bases in 03af950.

for base in [2, 5, 10, np.pi, np.e]:
formatter = mticker.LogFormatterExponent(base=base)
formatter.axis = FakeAxis()
vals = base**i
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
nose.tools.assert_equal(labels, expected_result)

# Should be a blank string for non-integer powers if labelOnlyBase=True
formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=True)
formatter.axis = FakeAxis()
nose.tools.assert_equal(formatter(10**0.1), '')

# Otherwise, non-integer powers should be nicely formatted
locs = np.array([0.1, 0.00001, np.pi, 0.2, -0.2, -0.00001])
i = range(len(locs))
expected_result = ['0.1', '1e-05', '3.14', '0.2', '-0.2', '-1e-05']
for base in [2, 5, 10, np.pi, np.e]:
formatter = mticker.LogFormatterExponent(base, labelOnlyBase=False)
formatter.axis = FakeAxis()
vals = base**locs
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
nose.tools.assert_equal(labels, expected_result)


Copy link
Member

Choose a reason for hiding this comment

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

and here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tacaswell - Thanks! Apparently I wasn't paying very much attention when I pushed this!

def test_use_offset():
for use_offset in [True, False]:
with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}):
Expand Down
11 changes: 4 additions & 7 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,10 @@ def __call__(self, x, pos=None):
isDecade = is_close_to_int(fx)
if not isDecade and self.labelOnlyBase:
s = ''
#if 0: pass
elif fx > 10000:
s = '%1.0e' % fx
#elif x<1: s = '$10^{%d}$'%fx
#elif x<1: s = '10^%d'%fx
elif fx < 1:
s = '%1.0e' % fx
elif abs(fx) > 10000:
s = '%1.0g' % fx
elif abs(fx) < 1:
s = '%1.0g' % fx
else:
s = self.pprint_val(fx, d)
if sign == -1:
Expand Down