From eec6c99d34d8ae36731176ea104f17aa8e9eace8 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Tue, 20 Nov 2018 21:06:54 -0500 Subject: [PATCH 1/8] Improved version based on gh comment --- lib/matplotlib/ticker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index ba1e7bc6cd93..e6f9b5bfecd9 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1230,6 +1230,7 @@ def __init__(self, unit="", places=None, sep=" "): self.unit = unit self.places = places self.sep = sep + self._usetex = rcParams['text.usetex'] def __call__(self, x, pos=None): s = "%s%s" % (self.format_eng(x), self.unit) @@ -1281,7 +1282,7 @@ def format_eng(self, num): pow10 += 3 prefix = self.ENG_PREFIXES[int(pow10)] - if rcParams['text.usetex']: + if self._usetex: formatted = "${mant:{fmt}}${sep}{prefix}".format( mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) else: From fc9a1effdca6cd3f9e8a8b72358306628da55562 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Tue, 20 Nov 2018 21:53:03 -0500 Subject: [PATCH 2/8] added public interface to usemathtext for EngFormatter --- lib/matplotlib/tests/test_ticker.py | 13 +++++++++++++ lib/matplotlib/tests/test_usetex.py | 15 --------------- lib/matplotlib/ticker.py | 19 ++++++++++++++++--- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 9dddb69cf764..5b0e48fac643 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -788,6 +788,19 @@ def test_params(self, input, expected): assert _formatter(input) == _exp_output +def test_engformatter_mathtext(): + fig, ax = plt.subplots() + ax.plot([0, 500, 1000], [0, 500, 1000]) + ax.set_xticks([0, 500, 1000]) + formatter = mticker.EngFormatter(useMathText=True) + 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'] + + 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 e6f9b5bfecd9..5babd75f15ff 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1200,7 +1200,7 @@ class EngFormatter(Formatter): 24: "Y" } - def __init__(self, unit="", places=None, sep=" "): + def __init__(self, unit="", places=None, sep=" ", useMathText=None): """ Parameters ---------- @@ -1230,7 +1230,20 @@ def __init__(self, unit="", places=None, sep=" "): self.unit = unit self.places = places self.sep = sep - self._usetex = rcParams['text.usetex'] + if useMathText is None: + useMathText = rcParams['axes.formatter.use_mathtext'] + self.set_useMathText(useMathText) + + 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) @@ -1282,7 +1295,7 @@ def format_eng(self, num): pow10 += 3 prefix = self.ENG_PREFIXES[int(pow10)] - if self._usetex: + if self._useMathText: formatted = "${mant:{fmt}}${sep}{prefix}".format( mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) else: From 732910c639cd86324f30327d4126a06d8e93761b Mon Sep 17 00:00:00 2001 From: pharshalp Date: Sun, 24 Feb 2019 11:10:08 -0500 Subject: [PATCH 3/8] Adding usetex, useMathText to EngFormatter + test --- lib/matplotlib/tests/test_ticker.py | 17 +++++++++-------- lib/matplotlib/ticker.py | 27 +++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 5b0e48fac643..af5cf876fd5f 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -788,17 +788,18 @@ def test_params(self, input, expected): assert _formatter(input) == _exp_output -def test_engformatter_mathtext(): +def test_engformatter_usetex_useMathText(): fig, ax = plt.subplots() ax.plot([0, 500, 1000], [0, 500, 1000]) ax.set_xticks([0, 500, 1000]) - formatter = mticker.EngFormatter(useMathText=True) - 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'] + 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): diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 5babd75f15ff..76f484d35fb6 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=" ", useMathText=None): + def __init__(self, unit="", places=None, sep=" ", usetex=None, + useMathText=None): """ Parameters ---------- @@ -1226,14 +1227,32 @@ def __init__(self, unit="", places=None, sep=" ", useMathText=None): * ``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 - if useMathText is None: - useMathText = rcParams['axes.formatter.use_mathtext'] + 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 @@ -1295,7 +1314,7 @@ def format_eng(self, num): pow10 += 3 prefix = self.ENG_PREFIXES[int(pow10)] - if self._useMathText: + if self._usetex or self._useMathText: formatted = "${mant:{fmt}}${sep}{prefix}".format( mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) else: From ab1b9cbfbe2d53a419b18bb95d1bcbba3ef7c438 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Sun, 24 Feb 2019 14:28:14 -0500 Subject: [PATCH 4/8] Making usetex, useMathText keyword only arguments --- lib/matplotlib/ticker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 76f484d35fb6..b6a99cff1bd3 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1200,7 +1200,7 @@ class EngFormatter(Formatter): 24: "Y" } - def __init__(self, unit="", places=None, sep=" ", usetex=None, + def __init__(self, unit="", places=None, sep=" ", *, usetex=None, useMathText=None): """ Parameters From e47772ed77e2923e87e12c1aada317936b3c21fd Mon Sep 17 00:00:00 2001 From: pharshalp Date: Sun, 24 Feb 2019 14:31:10 -0500 Subject: [PATCH 5/8] Removed extra spurious backtick --- lib/matplotlib/ticker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index b6a99cff1bd3..059b265e20c2 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1227,7 +1227,7 @@ def __init__(self, unit="", places=None, sep=" ", *, usetex=None, * ``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. From a5058e3961b44e329abdc96ffd7318cc495927a9 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Mon, 25 Feb 2019 16:26:36 -0500 Subject: [PATCH 6/8] Added a what's new entry --- .../engformatter_usetex_usemathtext.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 doc/users/next_whats_new/engformatter_usetex_usemathtext.rst 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..13d27b8ec1dd --- /dev/null +++ b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst @@ -0,0 +1,11 @@ +`EngFormatter` now accepts `usetex`, `useMathText` as keyword only arguments +`````````````````````````````````````````````````````````````````````````````````````````````` + +A public API has been added to `EngFormatter` 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). From b39adf47d75444e8fc106ec585a202d7c4453f66 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Mon, 25 Feb 2019 16:28:56 -0500 Subject: [PATCH 7/8] Fixed a typo --- doc/users/next_whats_new/engformatter_usetex_usemathtext.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst index 13d27b8ec1dd..c32e3c11361a 100644 --- a/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst +++ b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst @@ -1,7 +1,7 @@ `EngFormatter` now accepts `usetex`, `useMathText` as keyword only arguments `````````````````````````````````````````````````````````````````````````````````````````````` -A public API has been added to `EngFormatter` control how the numbers in the ticklabels will be rendered. +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']`. From e7dbc39a961138726636468b67305c09c87d4fc2 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Sat, 2 Mar 2019 17:57:54 -0500 Subject: [PATCH 8/8] Fix what's new based on gh comments --- .../engformatter_usetex_usemathtext.rst | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst index c32e3c11361a..bb8877a59b5b 100644 --- a/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst +++ b/doc/users/next_whats_new/engformatter_usetex_usemathtext.rst @@ -1,11 +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']`. +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). +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).