10000 Fixed LogFormatterExponent to correctly format negative exponents and… · matplotlib/matplotlib@1eb8aca · GitHub
[go: up one dir, main page]

Skip to content

Commit 1eb8aca

Browse files
committed
Fixed LogFormatterExponent to correctly format negative exponents and added tests
1 parent f3d97b9 commit 1eb8aca

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ def test_LogLocator():
5050
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
5151
assert_almost_equal(loc.tick_values(1, 100), test_value)
5252

53+
def test_LogFormatterExponent():
54+
class FakeAxis(object):
55+
"""Allow Formatter to be called without having a "full" plot set up."""
56+
def get_view_interval(self):
57+
return 1, 10
58+
59+
i = np.arange(-3, 4, dtype=float)
60+
expected_result = ['-3', '-2', '-1', '0', '1', '2', '3']
61+
for base in [2, 5, 10, np.pi]:
62+
formatter = mticker.LogFormatterExponent(base=base)
63+
formatter.axis = FakeAxis()
64+
vals = base**i
65+
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
66+
nose.tools.assert_equal(labels, expected_result)
67+
68+
# Should be a blank string for non-integer powers if labelOnlyBase=True
69+
formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=True)
70+
formatter.axis = FakeAxis()
71+
nose.tools.assert_equal(formatter(10**0.1), '')
72+
73+
# Otherwise, non-integer powers should be nicely formatted
74+
formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=False)
75+
formatter.axis = FakeAxis()
76+
nose.tools.assert_equal(formatter(10**0.1), '0.1')
77+
nose.tools.assert_equal(formatter(10**0.00001), '1e-05')
78+
nose.tools.assert_equal(formatter(10**-0.2), '-0.2')
5379

5480
def test_use_offset():
5581
for use_offset in [True, False]:

lib/matplotlib/ticker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,10 @@ def __call__(self, x, pos=None):
728728
isDecade = is_close_to_int(fx)
729729
if not isDecade and self.labelOnlyBase:
730730
s = ''
731-
elif fx > 10000:
732-
s = '%1.0e' % fx
733-
elif fx < 1:
734-
s = '%1.0e' % fx
731+
elif abs(fx) > 10000:
732+
s = '%1.0g' % fx
733+
elif abs(fx) < 1:
734+
s = '%1.0g' % fx
735735
else:
736736
s = self.pprint_val(fx, d)
737737
if sign == -1:

0 commit comments

Comments
 (0)
2905
0