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

Skip to content

Commit 4ba8903

Browse files
authored
Merge pull request #8322 from dstansby/min-exponent
ENH: Use scalars below a certain exponent in labes of log-scales axis
2 parents 70d5de6 + a76b044 commit 4ba8903

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Specify minimum value to format as scalar for ``LogFormatterMathtext``
2+
----------------------------------------------------------------------
3+
4+
``LogFormatterMathtext`` now includes the option to specify a minimum value
5+
exponent to format as a scalar (ie. 0.001 instead of 10^-3).

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@ def _validate_linestyle(ls):
11641164
'axes.formatter.use_locale': [False, validate_bool],
11651165
# Use the current locale to format ticks
11661166
'axes.formatter.use_mathtext': [False, validate_bool],
1167+
'axes.formatter.min_exponent': [0, validate_int], # minimum exponent to format in scientific notation
11671168
'axes.formatter.useoffset': [True, validate_bool],
11681169
'axes.formatter.offset_threshold': [4, validate_int],
11691170
'axes.unicode_minus': [True, validate_bool],

lib/matplotlib/tests/test_ticker.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,26 @@ def test_blank(self):
282282
assert formatter(10**0.1) == ''
283283

284284

285+
class TestLogFormatterMathtext():
286+
fmt = mticker.LogFormatterMathtext()
287+
test_data = [
288+
(0, 1, '$\\mathdefault{10^{0}}$'),
289+
(0, 1e-2, '$\\mathdefault{10^{-2}}$'),
290+
(0, 1e2, '$\\mathdefault{10^{2}}$'),
291+
(3, 1, '$\\mathdefault{1}$'),
292+
(3, 1e-2, '$\\mathdefault{0.01}$'),
293+
(3, 1e2, '$\\mathdefault{100}$'),
294+
(3, 1e-3, '$\\mathdefault{10^{-3}}$'),
295+
(3, 1e3, '$\\mathdefault{10^{3}}$'),
296+
]
297+
298+
@pytest.mark.parametrize('min_exponent, value, expected', test_data)
299+
def test_min_exponent(self, min_exponent, value, expected):
300+
with matplotlib.rc_context({'axes.formatter.min_exponent':
301+
min_exponent}):
302+
assert self.fmt(value) == expected
303+
304+
285305
class TestLogFormatterSciNotation(object):
286306
test_data = [
287307
(2, 0.03125, '$\\mathdefault{2^{-5}}$'),

lib/matplotlib/ticker.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ def _non_decade_format(self, sign_string, base, fx, usetex):
10751075
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
10761076
else:
10771077
return ('$%s$' % _mathdefault('%s%s^{%.2f}' %
1078-
(sign_string, base, fx)))
1078+
(sign_string, base, fx)))
10791079

10801080
def __call__(self, x, pos=None):
10811081
"""
@@ -1084,6 +1084,8 @@ def __call__(self, x, pos=None):
10841084
The position `pos` is ignored.
10851085
"""
10861086
usetex = rcParams['text.usetex']
1087+
min_exp = rcParams['axes.formatter.min_exponent']
1088+
10871089
if x == 0: # Symlog
10881090
if usetex:
10891091
return '$0$'
@@ -1099,6 +1101,8 @@ def __call__(self, x, pos=None):
10991101
is_x_decade = is_close_to_int(fx)
11001102
exponent = np.round(fx) if is_x_decade else np.floor(fx)
11011103
coeff = np.round(x / b ** exponent)
1104+
if is_x_decade:
1105+
fx = nearest_long(fx)
11021106

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

1114-
if not is_x_decade:
1118+
if np.abs(fx) < min_exp:
1119+
if usetex:
1120+
return r'${0}{1:g}$'.format(sign_string, x)
1121+
else:
1122+
return '${0}$'.format(_mathdefault(
1123+
'{0}{1:g}'.format(sign_string, x)))
1124+
elif not is_x_decade:
11151125
return self._non_decade_format(sign_string, base, fx, usetex)
11161126
else:
11171127
if usetex:

matplotlibrc.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ backend : $TEMPLATE_BACKEND
315315
# separator in the fr_FR locale.
316316
#axes.formatter.use_mathtext : False # When True, use mathtext for scientific
317317
# notation.
318+
#axes.formatter.min_exponent: 0 # minimum exponent to format in scientific notation
318319
#axes.formatter.useoffset : True # If True, the tick label formatter
319320
# will default to labeling ticks relative
320321
# to an offset when the data range is

0 commit comments

Comments
 (0)