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

Skip to content

Commit a6b5299

Browse files
committed
use scalars below a min exponent in LogFormatterMathtext
1 parent 0fadaaf commit a6b5299

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
@@ -1082,6 +1082,7 @@ def validate_animation_writer_path(p):
10821082
'axes.formatter.use_locale': [False, validate_bool],
10831083
# Use the current locale to format ticks
10841084
'axes.formatter.use_mathtext': [False, validate_bool],
1085+
'axes.formatter.min_exponent': [0, validate_int], # minimum exponent to format in scientific notation
10851086
'axes.formatter.useoffset': [True, validate_bool],
10861087
'axes.unicode_minus': [True, validate_bool],
10871088
'axes.color_cycle': [

lib/matplotlib/tests/test_ticker.py

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

254+
def test_LogFormatterMathtext_min_exponent():
255+
fmt = mticker.LogFormatterMathtext()
256+
257+
with matplotlib.rc_context({'axes.formatter.min_exponent': 0}):
258+
assert fmt(1) == '${10^{0}}$'
259+
assert fmt(1e-2) == '${10^{-2}}$'
260+
assert fmt(1e2) == '${10^{2}}$'
261+
262+
with matplotlib.rc_context({'axes.formatter.min_exponent': 3}):
263+
assert fmt(1) == '${1}$'
264+
assert fmt(1e-2) == '${0.01}$'
265+
assert fmt(1e2) == '${100}$'
266+
assert fmt(1e-3) == '${10^{-3}}$'
267+
assert fmt(1e3) == '${10^{3}}$'
254268

255269
def _pprint_helper(value, domain, expected):
256270
fmt = mticker.LogFormatter()

lib/matplotlib/ticker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ def __call__(self, x, pos=None):
959959
"""
960960
b = self._base
961961
usetex = rcParams['text.usetex']
962+
min_exp = rcParams['axes.formatter.min_exponent']
962963

963964
# only label the decades
964965
if x == 0:
@@ -969,6 +970,8 @@ def __call__(self, x, pos=None):
969970

970971
fx = math.log(abs(x)) / math.log(b)
971972
is_decade = is_close_to_int(fx)
973+
if is_decade:
974+
fx = nearest_long(fx)
972975

973976
sign_string = '-' if x < 0 else ''
974977

@@ -980,6 +983,12 @@ def __call__(self, x, pos=None):
980983

981984
if not is_decade and self.labelOnlyBase:
982985
return ''
986+
elif np.abs(fx) < min_exp:
987+
if usetex:
988+
return r'${0}{1:g}$'.format(sign_string, x)
989+
else:
990+
return '${0}$'.format(_mathdefault(
991+
'{0}{1:g}'.format(sign_string, x)))
983992
elif not is_decade:
984993
if usetex:
985994
return (r'$%s%s^{%.2f}$') % \

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 very

0 commit comments

Comments
 (0)