Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e3ef7d

Browse files
committed
FIX: formatting in LogFormatterExponent
The scale passed to the `pprint_val` method needs to also be scaled by the log so that the range used to format the tick labels matches the actual range of the tick labels (not the underlying values). reported via http://stackoverflow.com/questions/33975758/matplotlib-logformatterexponent-e-in-the-exponent-labels-of-cbar
1 parent 00b74bd commit 7e3ef7d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,28 @@ def test_SymmetricalLogLocator_set_params():
159159
nose.tools.assert_equal(sym.numticks, 8)
160160

161161

162+
def _logfe_helper(formatter, base, locs, i, expected_result):
163+
vals = base**locs
164+
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
165+
nose.tools.assert_equal(labels, expected_result)
166+
167+
162168
def test_LogFormatterExponent():
163169
class FakeAxis(object):
164170
"""Allow Formatter to be called without having a "full" plot set up."""
171+
def __init__(self, vmin=1, vmax=10):
172+
self.vmin = vmin
173+
self.vmax = vmax
174+
165175
def get_view_interval(self):
166-
return 1, 10
176+
return self.vmin, self.vmax
167177

168178
i = np.arange(-3, 4, dtype=float)
169179
expected_result = ['-3', '-2', '-1', '0', '1', '2', '3']
170-
for base in [2, 5, 10, np.pi, np.e]:
180+
for base in [2, 5.0, 10.0, np.pi, np.e]:
171181
formatter = mticker.LogFormatterExponent(base=base)
172-
formatter.axis = FakeAxis()
173-
vals = base**i
174-
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
175-
nose.tools.assert_equal(labels, expected_result)
182+
formatter.axis = FakeAxis(1, base**4)
183+
yield _logfe_helper, formatter, base, i, i, expected_result
176184

177185
# Should be a blank string for non-integer powers if labelOnlyBase=True
178186
formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=True)
@@ -185,10 +193,15 @@ def get_view_interval(self):
185193
expected_result = ['0.1', '1e-05', '3.14', '0.2', '-0.2', '-1e-05']
186194
for base in [2, 5, 10, np.pi, np.e]:
187195
formatter = mticker.LogFormatterExponent(base, labelOnlyBase=False)
188-
formatter.axis = FakeAxis()
189-
vals = base**locs
190-
labels = [formatter(x, pos) for (x, pos) in zip(vals, i)]
191-
nose.tools.assert_equal(labels, expected_result)
196+
formatter.axis = FakeAxis(1, base**10)
197+
yield _logfe_helper, formatter, base, locs, i, expected_result
198+
199+
expected_result = ['3', '5', '12', '42']
200+
locs = np.array([3, 5, 12, 42], dtype='float')
201+
for base in [2, 5.0, 10.0, np.pi, np.e]:
202+
formatter = mticker.LogFormatterExponent(base, labelOnlyBase=False)
203+
formatter.axis = FakeAxis(1, base**50)
204+
yield _logfe_helper, formatter, base, locs, i, expected_result
192205

193206

194207
def test_use_offset():

lib/matplotlib/ticker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ def __call__(self, x, pos=None):
768768
elif abs(fx) < 1:
769769
s = '%1.0g' % fx
770770
else:
771-
s = self.pprint_val(fx, d)
771+
fd = math.log(abs(d)) / math.log(b)
772+
s = self.pprint_val(fx, fd)
772773
if sign == -1:
773774
s = '-%s' % s
774775

0 commit comments

Comments
 (0)