FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use scalars below a certain exponent in labes of log-scales axis by zblz · Pull Request #6834 · matplotlib/matplotlib · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .template  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ def validate_animation_writer_path(p):
'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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,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, '${2^{-5}}$'),
Expand Down
14 changes: 12 additions & 2 deletions lib/matplotlib/ticker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,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 @@ -1093,6 +1093,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 @@ -1108,6 +1110,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 @@ -1120,7 +1124,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
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

Back | FazBrowse Home | New Git URL