Skip to content

correctly format ticklabels when EngFormatter is used with usetex = True #12847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2018
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