Skip to content

Commit

Permalink
Merge pull request #8322 from dstansby/min-exponent
Browse files Browse the repository at this point in the history
ENH: Use scalars below a certain exponent in labes of log-scales axis
  • Loading branch information
tacaswell committed Apr 6, 2017
2 parents 70d5de6 + a76b044 commit 4ba8903
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions doc/users/whats_new/min_log_scale_exponent.rst
@@ -0,0 +1,5 @@
Specify minimum value to format as scalar for ``LogFormatterMathtext``
----------------------------------------------------------------------

``LogFormatterMathtext`` now includes the option to specify a minimum value
exponent to format as a scalar (ie. 0.001 instead of 10^-3).
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Expand Up @@ -1164,6 +1164,7 @@ def _validate_linestyle(ls):
'axes.formatter.use_locale': [False, validate_bool],
# Use the current locale to format ticks
'axes.formatter.use_mathtext': [False, validate_bool],
'axes.formatter.min_exponent': [0, validate_int], # minimum exponent to format in scientific notation
'axes.formatter.useoffset': [True, validate_bool],
'axes.formatter.offset_threshold': [4, validate_int],
'axes.unicode_minus': [True, validate_bool],
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Expand Up @@ -282,6 +282,26 @@ def test_blank(self):
assert formatter(10**0.1) == ''


class TestLogFormatterMathtext():
fmt = mticker.LogFormatterMathtext()
test_data = [
(0, 1, '$\\mathdefault{10^{0}}$'),
(0, 1e-2, '$\\mathdefault{10^{-2}}$'),
(0, 1e2, '$\\mathdefault{10^{2}}$'),
(3, 1, '$\\mathdefault{1}$'),
(3, 1e-2, '$\\mathdefault{0.01}$'),
(3, 1e2, '$\\mathdefault{100}$'),
(3, 1e-3, '$\\mathdefault{10^{-3}}$'),
(3, 1e3, '$\\mathdefault{10^{3}}$'),
]

@pytest.mark.parametrize('min_exponent, value, expected', test_data)
def test_min_exponent(self, min_exponent, value, expected):
with matplotlib.rc_context({'axes.formatter.min_exponent':
min_exponent}):
assert self.fmt(value) == expected


class TestLogFormatterSciNotation(object):
test_data = [
(2, 0.03125, '$\\mathdefault{2^{-5}}$'),
Expand Down
14 changes: 12 additions & 2 deletions lib/matplotlib/ticker.py
Expand Up @@ -1075,7 +1075,7 @@ def _non_decade_format(self, sign_string, base, fx, usetex):
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
else:
return ('$%s$' % _mathdefault('%s%s^{%.2f}' %
(sign_string, base, fx)))
(sign_string, base, fx)))

def __call__(self, x, pos=None):
"""
Expand All @@ -1084,6 +1084,8 @@ def __call__(self, x, pos=None):
The position `pos` is ignored.
"""
usetex = rcParams['text.usetex']
min_exp = rcParams['axes.formatter.min_exponent']

if x == 0: # Symlog
if usetex:
return '$0$'
Expand All @@ -1099,6 +1101,8 @@ def __call__(self, x, pos=None):
is_x_decade = is_close_to_int(fx)
exponent = np.round(fx) if is_x_decade else np.floor(fx)
coeff = np.round(x / b ** exponent)
if is_x_decade:
fx = nearest_long(fx)

if self.labelOnlyBase and not is_x_decade:
return ''
Expand All @@ -1111,7 +1115,13 @@ def __call__(self, x, pos=None):
else:
base = '%s' % b

if not is_x_decade:
if np.abs(fx) < min_exp:
if usetex:
return r'${0}{1:g}$'.format(sign_string, x)
else:
return '${0}$'.format(_mathdefault(
'{0}{1:g}'.format(sign_string, x)))
elif not is_x_decade:
return self._non_decade_format(sign_string, base, fx, usetex)
else:
if usetex:
Expand Down
1 change: 1 addition & 0 deletions matplotlibrc.template
Expand Up @@ -315,6 +315,7 @@ backend : $TEMPLATE_BACKEND
# separator in the fr_FR locale.
#axes.formatter.use_mathtext : False # When True, use mathtext for scientific
# notation.
#axes.formatter.min_exponent: 0 # minimum exponent to format in scientific notation
#axes.formatter.useoffset : True # If True, the tick label formatter
# will default to labeling ticks relative
# to an offset when the data range is
Expand Down

0 comments on commit 4ba8903

Please sign in to comment.