Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5a7f2eb

Browse filesBrowse files
timhoffmMeeseeksDev[bot]
authored andcommitted
Backport PR #12856: added property usemathtext to EngFormatter
1 parent f6036c5 commit 5a7f2eb
Copy full SHA for 5a7f2eb

File tree

Expand file treeCollapse file tree

4 files changed

+63
-17
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+63
-17
lines changed
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
`EngFormatter` now accepts `usetex`, `useMathText` as keyword only arguments
2+
````````````````````````````````````````````````````````````````````````````
3+
4+
A public API has been added to `EngFormatter` to control how the numbers in the
5+
ticklabels will be rendered. By default, ``useMathText`` evaluates to
6+
``rcParams['axes.formatter.use_mathtext']`` and ``usetex`` evaluates to
7+
``rcParams['text.usetex']``.
8+
9+
If either is ``True`` then the numbers will be encapsulated by ``$`` signs.
10+
When using ``TeX`` this implies that the numbers will be shown in TeX's math
11+
font. When using mathtext, the ``$`` signs around numbers will ensure unicode
12+
rendering (as implied by mathtext). This will make sure that the minus signs
13+
in the ticks are rendered as the unicode-minus (U+2212) when using mathtext
14+
(without relying on the ``fix_minus`` method).

‎lib/matplotlib/tests/test_ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_ticker.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,20 @@ def test_params(self, input, expected):
788788
assert _formatter(input) == _exp_output
789789

790790

791+
def test_engformatter_usetex_useMathText():
792+
fig, ax = plt.subplots()
793+
ax.plot([0, 500, 1000], [0, 500, 1000])
794+
ax.set_xticks([0, 500, 1000])
795+
for formatter in (mticker.EngFormatter(usetex=True),
796+
mticker.EngFormatter(useMathText=True)):
797+
ax.xaxis.set_major_formatter(formatter)
798+
fig.canvas.draw()
799+
x_tick_label_text = [labl.get_text() for labl in ax.get_xticklabels()]
800+
# Checking if the dollar `$` signs have been inserted around numbers
801+
# in tick labels.
802+
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']
803+
804+
791805
class TestPercentFormatter(object):
792806
percent_data = [
793807
# Check explicitly set decimals over different intervals and values

‎lib/matplotlib/tests/test_usetex.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_usetex.py
-15Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,3 @@ def test_usetex():
3232
fontsize=24)
3333
ax.set_xticks([])
3434
ax.set_yticks([])
35-
36-
37-
@needs_usetex
38-
def test_usetex_engformatter():
39-
matplotlib.rcParams['text.usetex'] = True
40-
fig, ax = plt.subplots()
41-
ax.plot([0, 500, 1000], [0, 500, 1000])
42-
ax.set_xticks([0, 500, 1000])
43-
formatter = EngFormatter()
44-
ax.xaxis.set_major_formatter(formatter)
45-
fig.canvas.draw()
46-
x_tick_label_text = [label.get_text() for label in ax.get_xticklabels()]
47-
# Checking if the dollar `$` signs have been inserted around numbers
48-
# in tick label text.
49-
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+35-2Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,8 @@ class EngFormatter(Formatter):
12081208
24: "Y"
12091209
}
12101210

1211-
def __init__(self, unit="", places=None, sep=" "):
1211+
def __init__(self, unit="", places=None, sep=" ", *, usetex=None,
1212+
useMathText=None):
12121213
"""
12131214
Parameters
12141215
----------
@@ -1234,10 +1235,42 @@ def __init__(self, unit="", places=None, sep=" "):
12341235
* ``sep="\\N{THIN SPACE}"`` (``U+2009``);
12351236
* ``sep="\\N{NARROW NO-BREAK SPACE}"`` (``U+202F``);
12361237
* ``sep="\\N{NO-BREAK SPACE}"`` (``U+00A0``).
1238+
1239+
usetex : bool (default: None)
1240+
To enable/disable the use of TeX's math mode for rendering the
1241+
numbers in the formatter.
1242+
1243+
useMathText : bool (default: None)
1244+
To enable/disable the use mathtext for rendering the numbers in
1245+
the formatter.
12371246
"""
12381247
self.unit = unit
12391248
self.places = places
12401249
self.sep = sep
1250+
self.set_usetex(usetex)
1251+
self.set_useMathText(useMathText)
1252+
1253+
def get_usetex(self):
1254+
return self._usetex
1255+
1256+
def set_usetex(self, val):
1257+
if val is None:
1258+
self._usetex = rcParams['text.usetex']
1259+
else:
1260+
self._usetex = val
1261+
1262+
usetex = property(fget=get_usetex, fset=set_usetex)
1263+
1264+
def get_useMathText(self):
1265+
return self._useMathText
1266+
1267+
def set_useMathText(self, val):
1268+
if val is None:
1269+
self._useMathText = rcParams['axes.formatter.use_mathtext']
1270+
else:
1271+
self._useMathText = val
1272+
1273+
useMathText = property(fget=get_useMathText, fset=set_useMathText)
12411274

12421275
def __call__(self, x, pos=None):
12431276
s = "%s%s" % (self.format_eng(x), self.unit)
@@ -1289,7 +1322,7 @@ def format_eng(self, num):
12891322
pow10 += 3
12901323

12911324
prefix = self.ENG_PREFIXES[int(pow10)]
1292-
if rcParams['text.usetex']:
1325+
if self._usetex or self._useMathText:
12931326
formatted = "${mant:{fmt}}${sep}{prefix}".format(
12941327
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
12951328
else:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.