Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_usetex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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']
9 changes: 6 additions & 3 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down