File size: 1,106 Bytes
d2e75be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/usr/bin/env python3
"""Render a paper figure WITHOUT in-figure title/subtitle/footnote text (that text belongs in the LaTeX caption).
Usage: run_bare.py <plot_script.py> <out_dir>
Runs the script unchanged, with two patches: Figure.text becomes a no-op (in these scripts fig.text is used only for the
title, subtitle and footnotes; panel titles, axis labels and legends are untouched), and every savefig is redirected to
<out_dir>/<original name>_bare.<ext> with a tight bounding box, so the original outputs are never overwritten."""
import os, runpy, sys
import matplotlib
matplotlib.use("Agg")
from matplotlib.figure import Figure
script, out_dir = sys.argv[1], sys.argv[2]
orig_save = Figure.savefig
Figure.text = lambda self, *a, **k: None
def save(self, fname, *a, **k):
base, ext = os.path.splitext(os.path.basename(str(fname)))
k.update(bbox_inches="tight", pad_inches=0.05)
dest = os.path.join(out_dir, f"{base}_bare{ext}")
print("wrote", dest)
return orig_save(self, dest, *a, **k)
Figure.savefig = save
sys.argv = [script]
runpy.run_path(script, run_name="__main__")
|