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

Skip to content

Backport PR #12856 on branch v3.1.x (added property usemathtext to EngFormatter) #13574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/users/next_whats_new/engformatter_usetex_usemathtext.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
`EngFormatter` now accepts `usetex`, `useMathText` as keyword only arguments
````````````````````````````````````````````````````````````````````````````

A public API has been added to `EngFormatter` to control how the numbers in the
ticklabels will be rendered. By default, ``useMathText`` evaluates to
``rcParams['axes.formatter.use_mathtext']`` and ``usetex`` evaluates to
``rcParams['text.usetex']``.

If either is ``True`` then the numbers will be encapsulated by ``$`` signs.
When using ``TeX`` this implies that the numbers will be shown in TeX's math
font. When using mathtext, the ``$`` signs around numbers will ensure unicode
rendering (as implied by mathtext). This will make sure that the minus signs
in the ticks are rendered as the unicode-minus (U+2212) when using mathtext
(without relying on the ``fix_minus`` method).
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,20 @@ def test_params(self, input, expected):
assert _formatter(input) == _exp_output


def test_engformatter_usetex_useMathText():
fig, ax = plt.subplots()
ax.plot([0, 500, 1000], [0, 500, 1000])
ax.set_xticks([0, 500, 1000])
for formatter in (mticker.EngFormatter(usetex=True),
mticker.EngFormatter(useMathText=True)):
ax.xaxis.set_major_formatter(formatter)
fig.canvas.draw()
x_tick_label_text = [labl.get_text() for labl in ax.get_xticklabels()]
# Checking if the dollar `$` signs have been inserted around numbers
# in tick labels.
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']


class TestPercentFormatter(object):
percent_data = [
# Check explicitly set decimals over different intervals and values
Expand Down
15 changes: 0 additions & 15 deletions lib/matplotlib/tests/test_usetex.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,3 @@ def test_usetex():
fontsize=24)
ax.set_xticks([])
ax.set_yticks([])


@needs_usetex
def test_usetex_engformatter():
matplotlib.rcParams['text.usetex'] = True
fig, ax = plt.subplots()
ax.plot([0, 500, 1000], [0, 500, 1000])
ax.set_xticks([0, 500, 1000])
formatter = EngFormatter()
ax.xaxis.set_major_formatter(formatter)
fig.canvas.draw()
x_tick_label_text = [label.get_text() for label in ax.get_xticklabels()]
# Checking if the dollar `$` signs have been inserted around numbers
# in tick label text.
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']
37 changes: 35 additions & 2 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,8 @@ class EngFormatter(Formatter):
24: "Y"
}

def __init__(self, unit="", places=None, sep=" "):
def __init__(self, unit="", places=None, sep=" ", *, usetex=None,
useMathText=None):
"""
Parameters
----------
Expand All @@ -1234,10 +1235,42 @@ def __init__(self, unit="", places=None, sep=" "):
* ``sep="\\N{THIN SPACE}"`` (``U+2009``);
* ``sep="\\N{NARROW NO-BREAK SPACE}"`` (``U+202F``);
* ``sep="\\N{NO-BREAK SPACE}"`` (``U+00A0``).

usetex : bool (default: None)
To enable/disable the use of TeX's math mode for rendering the
numbers in the formatter.

useMathText : bool (default: None)
To enable/disable the use mathtext for rendering the numbers in
the formatter.
"""
self.unit = unit
self.places = places
self.sep = sep
self.set_usetex(usetex)
self.set_useMathText(useMathText)

def get_usetex(self):
return self._usetex

def set_usetex(self, val):
if val is None:
self._usetex = rcParams['text.usetex']
else:
self._usetex = val

usetex = property(fget=get_usetex, fset=set_usetex)

def get_useMathText(self):
return self._useMathText

def set_useMathText(self, val):
if val is None:
self._useMathText = rcParams['axes.formatter.use_mathtext']
else:
self._useMathText = val

useMathText = property(fget=get_useMathText, fset=set_useMathText)

def __call__(self, x, pos=None):
s = "%s%s" % (self.format_eng(x), self.unit)
Expand Down Expand Up @@ -1289,7 +1322,7 @@ def format_eng(self, num):
pow10 += 3

prefix = self.ENG_PREFIXES[int(pow10)]
if rcParams['text.usetex']:
if self._usetex or self._useMathText:
formatted = "${mant:{fmt}}${sep}{prefix}".format(
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
else:
Expand Down