diff --git a/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst new file mode 100644 index 000000000000..bb8877a59b5b --- /dev/null +++ b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst @@ -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). diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 9dddb69cf764..af5cf876fd5f 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -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 diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index 2c74a7531875..84af0fb790c9 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -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'] diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index ba1e7bc6cd93..059b265e20c2 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1200,7 +1200,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 ---------- @@ -1226,10 +1227,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) @@ -1281,7 +1314,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: