Skip to content

Commit 684a1ea

Browse files
authored
Merge pull request #12856 from pharshalp/engformatter_tex_fix
added property usemathtext to EngFormatter
2 parents 7875bc5 + e7dbc39 commit 684a1ea

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed
Lines 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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,20 @@ def test_params(self, input, expected):
786786
assert _formatter(input) == _exp_output
787787

788788

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

lib/matplotlib/tests/test_usetex.py

Lines 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

Lines 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)