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

Skip to content

Commit dc61940

Browse files
committed
use scalars below a min exponent in LogFormatterMathtext
1 parent 36f1353 commit dc61940

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ def validate_animation_writer_path(p):
10831083
'axes.formatter.use_locale': [False, validate_bool],
10841084
# Use the current locale to format ticks
10851085
'axes.formatter.use_mathtext': [False, validate_bool],
1086+
'axes.formatter.min_exponent': [0, validate_int], # minimum exponent to format in scientific notation
10861087
'axes.formatter.useoffset': [True, validate_bool],
10871088
'axes.unicode_minus': [True, validate_bool],
10881089
'axes.color_cycle': [

lib/matplotlib/tests/test_ticker.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,20 @@ def get_view_interval(self):
304304
formatter.axis = FakeAxis(1, base**50)
305305
yield _logfe_helper, formatter, base, locs, i, expected_result
306306

307+
def test_LogFormatterMathtext_min_exponent():
308+
fmt = mticker.LogFormatterMathtext()
309+
310+
with matplotlib.rc_context({'axes.formatter.min_exponent': 0}):
311+
assert fmt(1) == '${10^{0}}$'
312+
assert fmt(1e-2) == '${10^{-2}}$'
313+
assert fmt(1e2) == '${10^{2}}$'
314+
315+
with matplotlib.rc_context({'axes.formatter.min_exponent': 3}):
316+
assert fmt(1) == '${1}$'
317+
assert fmt(1e-2) == '${0.01}$'
318+
assert fmt(1e2) == '${100}$'
319+
assert fmt(1e-3) == '${10^{-3}}$'
320+
assert fmt(1e3) == '${10^{3}}$'
307321

308322
def test_LogFormatterSciNotation():
309323
test_cases = {

lib/matplotlib/ticker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ def __call__(self, x, pos=None):
10071007
"""
10081008
b = self._base
10091009
usetex = rcParams['text.usetex']
1010+
min_exp = rcParams['axes.formatter.min_exponent']
10101011

10111012
# only label the decades
10121013
if x == 0:
@@ -1019,6 +1020,8 @@ def __call__(self, x, pos=None):
10191020
is_decade = is_close_to_int(fx)
10201021
exponent = np.round(fx) if is_decade else np.floor(fx)
10211022
coeff = np.round(x / b ** exponent)
1023+
if is_decade:
1024+
fx = nearest_long(fx)
10221025

10231026
sign_string = '-' if x < 0 else ''
10241027

@@ -1031,6 +1034,12 @@ def __call__(self, x, pos=None):
10311034
if coeff in self.sublabel:
10321035
if not is_decade and self.labelOnlyBase:
10331036
return ''
1037+
elif np.abs(fx) < min_exp:
1038+
if usetex:
1039+
return r'${0}{1:g}$'.format(sign_string, x)
1040+
else:
1041+
return '${0}$'.format(_mathdefault(
1042+
'{0}{1:g}'.format(sign_string, x)))
10341043
elif not is_decade:
10351044
return self._non_decade_format(sign_string, base, fx, usetex)
10361045
else:

matplotlibrc.template

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

0 commit comments

Comments
 (0)