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

Skip to content

Commit fc9a1ef

Browse files
committed
added public interface to usemathtext for EngFormatter
1 parent eec6c99 commit fc9a1ef

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

lib/matplotlib/tests/test_ticker.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,19 @@ def test_params(self, input, expected):
788788
assert _formatter(input) == _exp_output
789789

790790

791+
def test_engformatter_mathtext():
792+
fig, ax = plt.subplots()
793+
ax.plot([0, 500, 1000], [0, 500, 1000])
794+
ax.set_xticks([0, 500, 1000])
795+
formatter = mticker.EngFormatter(useMathText=True)
796+
ax.xaxis.set_major_formatter(formatter)
797+
fig.canvas.draw()
798+
x_tick_label_text = [label.get_text() for label in ax.get_xticklabels()]
799+
# Checking if the dollar `$` signs have been inserted around numbers
800+
# in tick label text.
801+
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']
802+
803+
791804
class TestPercentFormatter(object):
792805
percent_data = [
793806
# 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: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ class EngFormatter(Formatter):
12001200
24: "Y"
12011201
}
12021202

1203-
def __init__(self, unit="", places=None, sep=" "):
1203+
def __init__(self, unit="", places=None, sep=" ", useMathText=None):
12041204
"""
12051205
Parameters
12061206
----------
@@ -1230,7 +1230,20 @@ def __init__(self, unit="", places=None, sep=" "):
12301230
self.unit = unit
12311231
self.places = places
12321232
self.sep = sep
1233-
self._usetex = rcParams['text.usetex']
1233+
if useMathText is None:
1234+
useMathText = rcParams['axes.formatter.use_mathtext']
1235+
self.set_useMathText(useMathText)
1236+
1237+
def get_useMathText(self):
1238+
return self._useMathText
1239+
1240+
def set_useMathText(self, val):
1241+
if val is None:
1242+
self._useMathText = rcParams['axes.formatter.use_mathtext']
1243+
else:
1244+
self._useMathText = val
1245+
1246+
useMathText = property(fget=get_useMathText, fset=set_useMathText)
12341247

12351248
def __call__(self, x, pos=None):
12361249
s = "%s%s" % (self.format_eng(x), self.unit)
@@ -1282,7 +1295,7 @@ def format_eng(self, num):
12821295
pow10 += 3
12831296

12841297
prefix = self.ENG_PREFIXES[int(pow10)]
1285-
if self._usetex:
1298+
if self._useMathText:
12861299
formatted = "${mant:{fmt}}${sep}{prefix}".format(
12871300
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
12881301
else:

0 commit comments

Comments
 (0)