|
8 | 8 | import matplotlib.pyplot as plt
|
9 | 9 | import matplotlib.axes as maxes
|
10 | 10 |
|
11 |
| -from matplotlib import rcParams |
12 |
| -rcParams['text.usetex'] = True |
13 | 11 |
|
| 12 | +plt.rcParams.update({"mathtext.fontset": "cm", "mathtext.rm": "serif"}) |
14 | 13 |
|
15 |
| -class Axes(maxes.Axes): |
| 14 | + |
| 15 | +@maxes.subplot_class_factory |
| 16 | +class LatexPreviewSubplot(maxes.Axes): |
16 | 17 | """
|
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. |
19 | 21 | """
|
20 | 22 |
|
21 |
| - def __init__(self, *args, usetex=False, preview=False, **kwargs): |
22 |
| - self.
10000
usetex = usetex |
| 23 | + def __init__(self, *args, preview=False, **kwargs): |
23 | 24 | self.preview = preview
|
24 | 25 | super().__init__(*args, **kwargs)
|
25 | 26 |
|
26 | 27 | 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}): |
29 | 29 | super().draw(renderer)
|
30 | 30 |
|
31 | 31 |
|
32 |
| -subplot = maxes.subplot_class_factory(Axes) |
33 |
| - |
34 |
| - |
35 | 32 | def test_window_extent(ax, usetex, preview):
|
36 | 33 |
|
37 |
| - va = "baseline" |
38 | 34 | ax.xaxis.set_visible(False)
|
39 | 35 | ax.yaxis.set_visible(False)
|
40 | 36 |
|
41 |
| - text_kw = dict(va=va, |
42 |
| - size=50, |
43 |
| - bbox=dict(pad=0., ec="k", fc="none")) |
44 |
| - |
45 | 37 | test_strings = ["lg", r"$\frac{1}{2}\pi$",
|
46 | 38 | r"$p^{3^A}$", r"$p_{3_2}$"]
|
47 | 39 |
|
48 | 40 | ax.axvline(0, color="r")
|
49 | 41 |
|
50 | 42 | for i, s in enumerate(test_strings):
|
51 |
| - |
52 | 43 | 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")) |
54 | 49 |
|
55 | 50 | ax.set_xlim(-0.1, 1.1)
|
56 | 51 | ax.set_ylim(-.8, 3.9)
|
57 | 52 |
|
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) |
59 | 57 |
|
60 | 58 |
|
61 | 59 | fig = plt.figure(figsize=(2 * 3, 6.5))
|
62 | 60 |
|
63 | 61 | for i, usetex, preview in [[0, False, False],
|
64 | 62 | [1, True, False],
|
65 | 63 | [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) |
67 | 65 | fig.add_subplot(ax)
|
68 | 66 | fig.subplots_adjust(top=0.85)
|
69 | 67 |
|
|
0 commit comments