From 61a0cdd7cab371818bf0e8a5c122175ed13a8680 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Tue, 20 Nov 2018 00:40:44 -0500 Subject: [PATCH 1/2] Make EngFormatter use math font when usetex is True --- lib/matplotlib/ticker.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 81a2ebfe81d4..fb39bfba9dc7 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -1283,9 +1283,12 @@ def format_eng(self, num): pow10 += 3 prefix = self.ENG_PREFIXES[int(pow10)] - - formatted = "{mant:{fmt}}{sep}{prefix}".format( - mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) + if rcParams['text.usetex']: + formatted = "${mant:{fmt}}${sep}{prefix}".format( + mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) + else: + formatted = "{mant:{fmt}}{sep}{prefix}".format( + mant=mant, sep=self.sep, prefix=prefix, fmt=fmt) return formatted From c40a7f9063efbcba449036fb637b9066cabe91ce Mon Sep 17 00:00:00 2001 From: pharshalp Date: Tue, 20 Nov 2018 17:25:36 -0500 Subject: [PATCH 2/2] Added a test for usetex+EngFormatter --- lib/matplotlib/tests/test_usetex.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index 644493279d30..2c74a7531875 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -5,6 +5,7 @@ import matplotlib from matplotlib.testing.decorators import image_comparison import matplotlib.pyplot as plt +from matplotlib.ticker import EngFormatter with warnings.catch_warnings(): @@ -31,3 +32,18 @@ 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']