8000 Merge pull request #16373 from anntzer/usetex-baseline-test · matplotlib/matplotlib@1cbb4cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cbb4cc

Browse files
authored
Merge pull request #16373 from anntzer/usetex-baseline-test
Fix usetex_baseline_test.
2 parents 731e53d + 9fa950c commit 1cbb4cc

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

examples/text_labels_and_annotations/usetex_baseline_test.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,60 @@
88
import matplotlib.pyplot as plt
99
import matplotlib.axes as maxes
1010

11-
from matplotlib import rcParams
12-
rcParams['text.usetex'] = True
1311

12+
plt.rcParams.update({"mathtext.fontset": "cm", "mathtext.rm": "serif"})
1413

15-
class Axes(maxes.Axes):
14+
15+
@maxes.subplot_class_factory
16+
class LatexPreviewSubplot(maxes.Axes):
1617
"""
17-
A hackish way to simultaneously draw texts w/ usetex=True and
18-
usetex=False in the same figure. It does not work in the ps backend.
18+
A hackish way to simultaneously draw texts with text.latex.preview=True and
19+
text.latex.preview=False in the same figure. It does not work with the ps
20+
backend.
1921
"""
2022

21-
def __init__(self, *args, usetex=False, preview=False, **kwargs):
22-
self. 10000 usetex = usetex
23+
def __init__(self, *args, preview=False, **kwargs):
2324
self.preview = preview
2425
super().__init__(*args, **kwargs)
2526

2627
def draw(self, renderer):
27-
with plt.rc_context({"text.usetex": self.usetex,
28-
"text.latex.preview": self.preview}):
28+
with plt.rc_context({"text.latex.preview": self.preview}):
2929
super().draw(renderer)
3030

3131

32-
subplot = maxes.subplot_class_factory(Axes)
33-
34-
3532
def test_window_extent(ax, usetex, preview):
3633

37-
va = "baseline"
3834
ax.xaxis.set_visible(False)
3935
ax.yaxis.set_visible(False)
4036

41-
text_kw = dict(va=va,
42-
size=50,
43-
bbox=dict(pad=0., ec="k", fc="none"))
44-
4537
test_strings = ["lg", r"$\frac{1}{2}\pi$",
4638
r"$p^{3^A}$", r"$p_{3_2}$"]
4739

4840
ax.axvline(0, color="r")
4941

5042
for i, s in enumerate(test_strings):
51-
5243
ax.axhline(i, color="r")
53-
ax.text(0., 3 - i, s, **text_kw)
44+
ax.text(0., 3 - i, s,
45+
usetex=usetex,
46+
verticalalignment="baseline",
47+
size=50,
48+
bbox=dict(pad=0, ec="k", fc="none"))
5449

5550
ax.set_xlim(-0.1, 1.1)
5651
ax.set_ylim(-.8, 3.9)
5752

58-
ax.set_title("usetex=%s\npreview=%s" % (str(usetex), str(preview)))
53+
title = f"usetex={usetex}\n"
54+
if usetex:
55+
title += f"preview={preview}"
56+
ax.set_title(title)
5957

6058

6159
fig = plt.figure(figsize=(2 * 3, 6.5))
6260

6361
for i, usetex, preview in [[0, False, False],
6462
[1, True, False],
6563
[2, True, True]]:
66-
ax = subplot(fig, 1, 3, i + 1, usetex=usetex, preview=preview)
64+
ax = LatexPreviewSubplot(fig, 1, 3, i + 1, preview=preview)
6765
fig.add_subplot(ax)
6866
fig.subplots_adjust(top=0.85)
6967

0 commit comments

Comments
 (0)
0