8000 Merge pull request #2691 from joferkington/fix-LogFormatterExponent · matplotlib/matplotlib@35e1823 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35e1823

Browse files
committed
Merge pull request #2691 from joferkington/fix-LogFormatterExponent
Change LogFormatterExponent to consistently format negative exponents
2 parents 70cc699 + bad39f1 commit 35e1823

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ def test_LogLocator():
5151
assert_almost_equal(loc.tick_values(1, 100), test_value)
5252

5353

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

lib/matplotlib/ticker.py

Lines changed: 4 additions & 7 deletions
735
Original file line numberDiff line numberDiff line change
@@ -728,13 +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-
#if 0: pass
732-
elif fx > 10000:
733-
s = '%1.0e' % fx
734-
#elif x<1: s = '$10^{%d}$'%fx
-
#elif x<1: s = '10^%d'%fx
736-
elif fx < 1:
737-
s = '%1.0e' % fx
731+
elif abs(fx) > 10000:
732+
s = '%1.0g' % fx
733+
elif abs(fx) < 1:
734+
s = '%1.0g' % fx
738735
else:
739736
s = self.pprint_val(fx, d)
740737
if sign == -1:

0 commit comments

Comments
 (0)
0