From f3d97b9ae25574f8b355904df4a4b7c931d14ca4 Mon Sep 17 00:00:00 2001 From: Joe Kington Date: Fri, 20 Dec 2013 15:11:18 -0600 Subject: [PATCH 1/4] Removed commented-out portions --- lib/matplotlib/ticker.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 9c4eb48d5422..a0057faaac7b 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -728,11 +728,8 @@ 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 else: From 1eb8aca39664c023a8a2445bd04f1b211f0c6ebc Mon Sep 17 00:00:00 2001 From: Joe Kington Date: Fri, 20 Dec 2013 21:48:36 -0600 Subject: [PATCH 2/4] Fixed LogFormatterExponent to correctly format negative exponents and added tests --- lib/matplotlib/tests/test_ticker.py | 26 ++++++++++++++++++++++++++ lib/matplotlib/ticker.py | 8 ++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index e38b8986907d..0a4b5500900d 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -50,6 +50,32 @@ def test_LogLocator(): test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.]) assert_almost_equal(loc.tick_values(1, 100), test_value) +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'] + for base in [2, 5, 10, np.pi]: + 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 + formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=False) + formatter.axis = FakeAxis() + nose.tools.assert_equal(formatter(10**0.1), '0.1') + nose.tools.assert_equal(formatter(10**0.00001), '1e-05') + nose.tools.assert_equal(formatter(10**-0.2), '-0.2') def test_use_offset(): for use_offset in [True, False]: diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index a0057faaac7b..1ee55f8e08fe 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -728,10 +728,10 @@ def __call__(self, x, pos=None): isDecade = is_close_to_int(fx) if not isDecade and self.labelOnlyBase: s = '' - elif fx > 10000: - s = '%1.0e' % 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: From 03af9506befba7d0781bdbd567cb33b560e83dd8 Mon Sep 17 00:00:00 2001 From: Joe Kington Date: Fri, 10 Jan 2014 13:53:28 -0600 Subject: [PATCH 3/4] Make LogFormatterExponent tests check more non-integer ticks and bases --- lib/matplotlib/tests/test_ticker.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 0a4b5500900d..912e675b4f8e 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -58,7 +58,7 @@ def get_view_interval(self): i = np.arange(-3, 4, dtype=float) expected_result = ['-3', '-2', '-1', '0', '1', '2', '3'] - for base in [2, 5, 10, np.pi]: + for base in [2, 5, 10, np.pi, np.e]: formatter = mticker.LogFormatterExponent(base=base) formatter.axis = FakeAxis() vals = base**i @@ -71,11 +71,15 @@ def get_view_interval(self): nose.tools.assert_equal(formatter(10**0.1), '') # Otherwise, non-integer powers should be nicely formatted - formatter = mticker.LogFormatterExponent(base=10, labelOnlyBase=False) - formatter.axis = FakeAxis() - nose.tools.assert_equal(formatter(10**0.1), '0.1') - nose.tools.assert_equal(formatter(10**0.00001), '1e-05') - nose.tools.assert_equal(formatter(10**-0.2), '-0.2') + 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=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) def test_use_offset(): for use_offset in [True, False]: From bad39f1ce5b9e614354ed730c23b522e4b593cf9 Mon Sep 17 00:00:00 2001 From: Joe Kington Date: Fri, 10 Jan 2014 13:54:31 -0600 Subject: [PATCH 4/4] PEP8 fixes for LogFormatterExponent --- lib/matplotlib/tests/test_ticker.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 912e675b4f8e..6d17f7090189 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -50,6 +50,7 @@ def test_LogLocator(): test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.]) assert_almost_equal(loc.tick_values(1, 100), test_value) + def test_LogFormatterExponent(): class FakeAxis(object): """Allow Formatter to be called without having a "full" plot set up.""" @@ -75,12 +76,13 @@ def get_view_interval(self): 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=base, labelOnlyBase=False) + 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) + def test_use_offset(): for use_offset in [True, False]: with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}):