Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8b425d

Browse files
authored
Merge pull request #12847 from pharshalp/engformatter_tex_fix
correctly format ticklabels when EngFormatter is used with usetex = True
2 parents 23dd2ad + c40a7f9 commit f8b425d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/matplotlib/tests/test_usetex.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import matplotlib
66
from matplotlib.testing.decorators import image_comparison
77
import matplotlib.pyplot as plt
8+
from matplotlib.ticker import EngFormatter
89

910

1011
with warnings.catch_warnings():
@@ -31,3 +32,18 @@ def test_usetex():
3132
fontsize=24)
3233
ax.set_xticks([])
3334
ax.set_yticks([])
35+
36+
37+
@needs_usetex
38+
def test_usetex_engformatter():
39+
matplotlib.rcParams['text.usetex'] = True
40+
fig, ax = plt.subplots()
41+
ax.plot([0, 500, 1000], [0, 500, 1000])
42+
ax.set_xticks([0, 500, 1000])
43+
formatter = EngFormatter()
44+
ax.xaxis.set_major_formatter(formatter)
45+
fig.canvas.draw()
46+
x_tick_label_text = [label.get_text() for label in ax.get_xticklabels()]
47+
# Checking if the dollar `$` signs have been inserted around numbers
48+
# in tick label text.
49+
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']

lib/matplotlib/ticker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,12 @@ def format_eng(self, num):
12831283
pow10 += 3
12841284

12851285
prefix = self.ENG_PREFIXES[int(pow10)]
1286-
1287-
formatted = "{mant:{fmt}}{sep}{prefix}".format(
1288-
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
1286+
if rcParams['text.usetex']:
1287+
formatted = "${mant:{fmt}}${sep}{prefix}".format(
1288+
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
1289+
else:
1290+
formatted = "{mant:{fmt}}{sep}{prefix}".format(
1291+
mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
12891292

12901293
return formatted
12911294

0 commit comments

Comments
 (0)