Skip to content

Commit c6dda2d

Browse files
committed
Merge pull request #5594 from tacaswell/fix_logformatterexponent
FIX: formatting in LogFormatterExponent
2 parents b7c7c5e + f9c8aa1 commit c6dda2d

File tree

2 files changed

+177
-15
lines changed

2 files changed

+177
-15
lines changed

lib/matplotlib/tests/test_ticker.py

+169-10
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,161 @@ 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
205+
206+
207+
def _pprint_helper(value, domain, expected):
208+
fmt = mticker.LogFormatter()
209+
label = fmt.pprint_val(value, domain)
210+
nose.tools.assert_equal(label, expected)
211+
212+
213+
def test_logformatter_pprint():
214+
test_cases = (
215+
(3.141592654e-05, 0.001, '3.142e-5'),
216+
(0.0003141592654, 0.001, '3.142e-4'),
217+
(0.003141592654, 0.001, '3.142e-3'),
218+
(0.03141592654, 0.001, '3.142e-2'),
219+
(0.3141592654, 0.001, '3.142e-1'),
220+
(3.141592654, 0.001, '3.142'),
221+
(31.41592654, 0.001, '3.142e1'),
222+
(314.1592654, 0.001, '3.142e2'),
223+
(3141.592654, 0.001, '3.142e3'),
224+
(31415.92654, 0.001, '3.142e4'),
225+
(314159.2654, 0.001, '3.142e5'),
226+
(1e-05, 0.001, '1e-5'),
227+
(0.0001, 0.001, '1e-4'),
228+
(0.001, 0.001, '1e-3'),
229+
(0.01, 0.001, '1e-2'),
230+
(0.1, 0.001, '1e-1'),
231+
(1, 0.001, '1'),
232+
(10, 0.001, '10'),
233+
(100, 0.001, '100'),
234+
(1000, 0.001, '1000'),
235+
(10000, 0.001, '1e4'),
236+
(100000, 0.001, '1e5'),
237+
(3.141592654e-05, 0.015, '0'),
238+
(0.0003141592654, 0.015, '0'),
239+
(0.003141592654, 0.015, '0.003'),
240+
(0.03141592654, 0.015, '0.031'),
241+
(0.3141592654, 0.015, '0.314'),
242+
(3.141592654, 0.015, '3.142'),
243+
(31.41592654, 0.015, '31.416'),
244+
(314.1592654, 0.015, '314.159'),
245+
(3141.592654, 0.015, '3141.593'),
246+
(31415.92654, 0.015, '31415.927'),
247+
(314159.2654, 0.015, '314159.265'),
248+
(1e-05, 0.015, '0'),
249+
(0.0001, 0.015, '0'),
250+
(0.001, 0.015, '0.001'),
251+
(0.01, 0.015, '0.01'),
252+
(0.1, 0.015, '0.1'),
253+
(1, 0.015, '1'),
254+
(10, 0.015, '10'),
255+
(100, 0.015, '100'),
256+
(1000, 0.015, '1000'),
257+
(10000, 0.015, '10000'),
258+
(100000, 0.015, '100000'),
259+
(3.141592654e-05, 0.5, '0'),
260+
(0.0003141592654, 0.5, '0'),
261+
(0.003141592654, 0.5, '0.003'),
262+
(0.03141592654, 0.5, '0.031'),
263+
(0.3141592654, 0.5, '0.314'),
264+
(3.141592654, 0.5, '3.142'),
265+
(31.41592654, 0.5, '31.416'),
266+
(314.1592654, 0.5, '314.159'),
267+
(3141.592654, 0.5, '3141.593'),
268+
(31415.92654, 0.5, '31415.927'),
269+
(314159.2654, 0.5, '314159.265'),
270+
(1e-05, 0.5, '0'),
271+
(0.0001, 0.5, '0'),
272+
(0.001, 0.5, '0.001'),
273+
(0.01, 0.5, '0.01'),
274+
(0.1, 0.5, '0.1'),
275+
(1, 0.5, '1'),
276+
(10, 0.5, '10'),
277+
(100, 0.5, '100'),
278+
(1000, 0.5, '1000'),
279+
(10000, 0.5, '10000'),
280+
(100000, 0.5, '100000'),
281+
(3.141592654e-05, 5, '0'),
282+
(0.0003141592654, 5, '0'),
283+
(0.003141592654, 5, '0'),
284+
(0.03141592654, 5, '0.03'),
285+
(0.3141592654, 5, '0.31'),
286+
(3.141592654, 5, '3.14'),
287+
(31.41592654, 5, '31.42'),
288+
(314.1592654, 5, '314.16'),
289+
(3141.592654, 5, '3141.59'),
290+
(31415.92654, 5, '31415.93'),
291+
(314159.2654, 5, '314159.27'),
292+
(1e-05, 5, '0'),
293+
(0.0001, 5, '0'),
294+
(0.001, 5, '0'),
295+
(0.01, 5, '0.01'),
296+
(0.1, 5, '0.1'),
297+
(1, 5, '1'),
298+
(10, 5, '10'),
299+
(100, 5, '100'),
300+
(1000, 5, '1000'),
301+
(10000, 5, '10000'),
302+
(100000, 5, '100000'),
303+
(3.141592654e-05, 100, '0'),
304+
(0.0003141592654, 100, '0'),
305+
(0.003141592654, 100, '0'),
306+
(0.03141592654, 100, '0'),
307+
(0.3141592654, 100, '0.3'),
308+
(3.141592654, 100, '3.1'),
309+
(31.41592654, 100, '31.4'),
310+
(314.1592654, 100, '314.2'),
311+
(3141.592654, 100, '3141.6'),
312+
(31415.92654, 100, '31415.9'),
313+
(314159.2654, 100, '314159.3'),
314+
(1e-05, 100, '0'),
315+
(0.0001, 100, '0'),
316+
(0.001, 100, '0'),
317+
(0.01, 100, '0'),
318+
(0.1, 100, '0.1'),
319+
(1, 100, '1'),
320+
(10, 100, '10'),
321+
(100, 100, '100'),
322+
(1000, 100, '1000'),
323+
(10000, 100, '10000'),
324+
(100000, 100, '100000'),
325+
(3.141592654e-05, 1000000.0, '3.1e-5'),
326+
(0.0003141592654, 1000000.0, '3.1e-4'),
327+
(0.003141592654, 1000000.0, '3.1e-3'),
328+
(0.03141592654, 1000000.0, '3.1e-2'),
329+
(0.3141592654, 1000000.0, '3.1e-1'),
330+
(3.141592654, 1000000.0, '3.1'),
331+
(31.41592654, 1000000.0, '3.1e1'),
332+
(314.1592654, 1000000.0, '3.1e2'),
333+
(3141.592654, 1000000.0, '3.1e3'),
334+
(31415.92654, 1000000.0, '3.1e4'),
335+
(314159.2654, 1000000.0, '3.1e5'),
336+
(1e-05, 1000000.0, '1e-5'),
337+
(0.0001, 1000000.0, '1e-4'),
338+
(0.001, 1000000.0, '1e-3'),
339+
(0.01, 1000000.0, '1e-2'),
340+
(0.1, 1000000.0, '1e-1'),
341+
(1, 1000000.0, '1'),
342+
(10, 1000000.0, '10'),
343+
(100, 1000000.0, '100'),
344+
(1000, 1000000.0, '1000'),
345+
(10000, 1000000.0, '1e4'),
346+
(100000, 1000000.0, '1e5')
347+
)
348+
349+
for value, domain, expected in test_cases:
350+
yield _pprint_helper, value, domain, expected
192351

193352

194353
def test_use_offset():

lib/matplotlib/ticker.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,15 @@ def pprint_val(self, x, d):
747747
else:
748748
fmt = '%1.3f'
749749
s = fmt % x
750-
#print d, x, fmt, s
750+
751751
tup = s.split('e')
752752
if len(tup) == 2:
753753
mantissa = tup[0].rstrip('0').rstrip('.')
754-
sign = tup[1][0].replace('+', '')
755-
exponent = tup[1][1:].lstrip('0')
756-
s = '%se%s%s' % (mantissa, sign, exponent)
754+
exponent = int(tup[1])
755+
if exponent:
756+
s = '%se%d' % (mantissa, exponent)
757+
else:
758+
s = mantissa
757759
else:
758760
s = s.rstrip('0').rstrip('.')
759761
return s
@@ -784,7 +786,8 @@ def __call__(self, x, pos=None):
784786
elif abs(fx) < 1:
785787
s = '%1.0g' % fx
786788
else:
787-
s = self.pprint_val(fx, d)
789+
fd = math.log(abs(d)) / math.log(b)
790+
s = self.pprint_val(fx, fd)
788791
if sign == -1:
789792
s = '-%s' % s
790793

0 commit comments

Comments
 (0)