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

Skip to content

Commit e293230

Browse files
committed
use scalars below a min exponent in LogFormatterMathtext
1 parent 6a46f04 commit e293230

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@ def validate_animation_writer_path(p):
10931093
'axes.formatter.use_locale': [False, validate_bool],
10941094
# Use the current locale to format ticks
10951095
'axes.formatter.use_mathtext': [False, validate_bool],
1096+
'axes.formatter.min_exponent': [0, validate_int], # minimum exponent to format in scientific notation
10961097
'axes.formatter.useoffset': [True, validate_bool],
10971098
'axes.formatter.offset_threshold': [4, validate_int],
10981099
'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
@@ -332,6 +332,26 @@ def test_blank(self):
332332
assert formatter(10**0.1) == ''
333333

334334

335+
class TestLogFormatterMathtext():
336+
fmt = mticker.LogFormatterMathtext()
337+
test_data = [
338+
(0, 1, '${10^{0}}$'),
339+
(0, 1e-2, '${10^{-2}}$'),
340+
(0, 1e2, '${10^{2}}$'),
341+
(3, 1, '${1}$'),
342+
(3, 1e-2, '${0.01}$'),
343+
(3, 1e2, '${100}$'),
344+
(3, 1e-3, '${10^{-3}}$'),
345+
(3, 1e3, '${10^{3}}$'),
346+
]
347+
348+
@pytest.mark.parametrize('min_exponent, value, expected', test_data)
349+
def test_min_exponent(self, min_exponent, value, expected):
350+
with matplotlib.rc_context({'axes.formatter.min_exponent':
351+
min_exponent}):
352+
assert self.fmt(value) == expected
353+
354+
335355
class TestLogFormatterSciNotation(object):
336356
test_data = [
337357
(2, 0.03125, '${2^{-5}}$'),

lib/matplotlib/ticker.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ def _non_decade_format(self, sign_string, base, fx, usetex):
10841084
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
10851085
else:
10861086
return ('$%s$' % _mathdefault('%s%s^{%.2f}' %
1087-
(sign_string, base, fx)))
1087+
(sign_string, base, fx)))
10881088

10891089
def __call__(self, x, pos=None):
10901090
"""
@@ -1093,6 +1093,9 @@ def __call__(self, x, pos=None):
10931093
The position `pos` is ignored.
10941094
"""
10951095
usetex = rcParams['text.usetex']
1096+
min_exp = rcParams['axes.formatter.min_exponent']
1097+
1098+
# only label the decades
10961099
if x == 0: # Symlog
10971100
if usetex:
10981101
return '$0$'
@@ -1108,6 +1111,8 @@ def __call__(self, x, pos=None):
11081111
is_x_decade = is_close_to_int(fx)
11091112
exponent = np.round(fx) if is_x_decade else np.floor(fx)
11101113
coeff = np.round(x / b ** exponent)
1114+
if is_x_decade:
1115+
fx = nearest_long(fx)
11111116

11121117
if self.labelOnlyBase and not is_x_decade:
11131118
return ''
@@ -1120,8 +1125,26 @@ def __call__(self, x, pos=None):
11201125
else:
11211126
base = '%s' % b
11221127

1123-
if not is_x_decade:
1124-
return self._non_decade_format(sign_string, base, fx, usetex)
1128+
if coeff in self.sublabel:
1129+
if not is_x_decade and self.labelOnlyBase:
1130+
return ''
1131+
elif np.abs(fx) < min_exp:
1132+
if usetex:
1133+
return r'${0}{1:g}$'.format(sign_string, x)
1134+
else:
1135+
return '${0}$'.format(_mathdefault(
1136+
'{0}{1:g}'.format(sign_string, x)))
1137+
elif not is_x_decade:
1138+
return self._non_decade_format(sign_string, base, fx, usetex)
1139+
else:
1140+
if usetex:
1141+
return (r'$%s%s^{%d}$') % (sign_string,
1142+
base,
1143+
nearest_long(fx))
1144+
else:
1145+
return ('$%s$' % _mathdefault(
1146+
'%s%s^{%d}' %
1147+
(sign_string, base, nearest_long(fx))))
11251148
else:
11261149
if usetex:
11271150
return (r'$%s%s^{%d}$') % (sign_string,

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)