From 9fa950cf840ee5806a0402349ffe586a1865f747 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 30 Jan 2020 11:52:52 +0100 Subject: [PATCH] Fix usetex_baseline_test. The previous version didn't actually compare usetex=False and usetex=True because usetex state is stored in the text instance, so fix that. (The custom axes subclass remains necessary to compare text.latex.preview=False and =True.) Also misc. cleanups. --- .../usetex_baseline_test.py | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/examples/text_labels_and_annotations/usetex_baseline_test.py b/examples/text_labels_and_annotations/usetex_baseline_test.py index 349fa5915b06..84db23ebd1b9 100644 --- a/examples/text_labels_and_annotations/usetex_baseline_test.py +++ b/examples/text_labels_and_annotations/usetex_baseline_test.py @@ -8,54 +8,52 @@ import matplotlib.pyplot as plt import matplotlib.axes as maxes -from matplotlib import rcParams -rcParams['text.usetex'] = True +plt.rcParams.update({"mathtext.fontset": "cm", "mathtext.rm": "serif"}) -class Axes(maxes.Axes): + +@maxes.subplot_class_factory +class LatexPreviewSubplot(maxes.Axes): """ - A hackish way to simultaneously draw texts w/ usetex=True and - usetex=False in the same figure. It does not work in the ps backend. + A hackish way to simultaneously draw texts with text.latex.preview=True and + text.latex.preview=False in the same figure. It does not work with the ps + backend. """ - def __init__(self, *args, usetex=False, preview=False, **kwargs): - self.usetex = usetex + def __init__(self, *args, preview=False, **kwargs): self.preview = preview super().__init__(*args, **kwargs) def draw(self, renderer): - with plt.rc_context({"text.usetex": self.usetex, - "text.latex.preview": self.preview}): + with plt.rc_context({"text.latex.preview": self.preview}): super().draw(renderer) -subplot = maxes.subplot_class_factory(Axes) - - def test_window_extent(ax, usetex, preview): - va = "baseline" ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) - text_kw = dict(va=va, - size=50, - bbox=dict(pad=0., ec="k", fc="none")) - test_strings = ["lg", r"$\frac{1}{2}\pi$", r"$p^{3^A}$", r"$p_{3_2}$"] ax.axvline(0, color="r") for i, s in enumerate(test_strings): - ax.axhline(i, color="r") - ax.text(0., 3 - i, s, **text_kw) + ax.text(0., 3 - i, s, + usetex=usetex, + verticalalignment="baseline", + size=50, + bbox=dict(pad=0, ec="k", fc="none")) ax.set_xlim(-0.1, 1.1) ax.set_ylim(-.8, 3.9) - ax.set_title("usetex=%s\npreview=%s" % (str(usetex), str(preview))) + title = f"usetex={usetex}\n" + if usetex: + title += f"preview={preview}" + ax.set_title(title) fig = plt.figure(figsize=(2 * 3, 6.5)) @@ -63,7 +61,7 @@ def test_window_extent(ax, usetex, preview): for i, usetex, preview in [[0, False, False], [1, True, False], [2, True, True]]: - ax = subplot(fig, 1, 3, i + 1, usetex=usetex, preview=preview) + ax = LatexPreviewSubplot(fig, 1, 3, i + 1, preview=preview) fig.add_subplot(ax) fig.subplots_adjust(top=0.85)