From a290c98880e3e4a21fe042cb3efd66c795ecc5d6 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 15 Feb 2026 04:05:28 -0600 Subject: [PATCH] scripts: plotmpl.py: Added --w/hpad/w/hspace for low-level pad control These just expose the low-level w/hpad and w/hspace controls available in matplotlib's constrained_layout. --- I think something funky might be going on with matplotlib's constrained_layout. I've noticed a relatively annoying amount of padding as the number of plots in the grid grow quite large. Though as is usual with plotmpl.py, this may just be my own fault with the amount of hacks being applied. --w/hpad and --w/hspace provide a temporary workaround by overriding the low-level padding controls in matplotlib's constrained_layout (--w/hpad should probably be preferred, --w/hspace seems to be a legacy option). Though, while a temporary solution, these are probably a good idea to keep around for easy tweaking of plot padding. --- scripts/plot.py | 6 ++++-- scripts/plotmpl.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/scripts/plot.py b/scripts/plot.py index 569073bb..79c5f92c 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -2135,14 +2135,16 @@ if __name__ == "__main__": nargs='?', type=lambda x: int(x, 0), const=0, - help="Width in columns. <=0 uses the terminal width. Defaults " + help="Width in columns. <=0 uses the terminal width. In subplots " + "this instead expresses a ratio of the current grid. Defaults " "to min(terminal, 80).") parser.add_argument( '-H', '--height', nargs='?', type=lambda x: int(x, 0), const=..., # handles shell prompt spacing, which is a bit subtle - help="Height in rows. <=0 uses the terminal height. Defaults " + help="Height in rows. <=0 uses the terminal height. In subplots " + "this instead expresses a ratio of the current grid. Defaults " "to 17.") parser.add_argument( '-X', '--xlim', diff --git a/scripts/plotmpl.py b/scripts/plotmpl.py index b838a762..1ab7d7fb 100755 --- a/scripts/plotmpl.py +++ b/scripts/plotmpl.py @@ -850,6 +850,10 @@ def main(csv_paths, output, *, points_and_lines=False, width=WIDTH, height=HEIGHT, + wpad=None, + hpad=None, + wspace=None, + hspace=None, xlim=(None,None), ylim=(None,None), xlim_stddev=(None,None), @@ -1064,6 +1068,11 @@ def main(csv_paths, output, *, layout='constrained', # we need a linewidth to keep xkcd mode happy linewidth=8 if xkcd else 0) + fig.get_layout_engine().set( + w_pad=wpad/72 if wpad is not None else None, + h_pad=hpad/72 if hpad is not None else None, + wspace=wspace if wspace is not None else 0, + hspace=hspace if hspace is not None else 0) gs = fig.add_gridspec( grid.height @@ -1560,11 +1569,33 @@ if __name__ == "__main__": parser.add_argument( '-W', '--width', type=lambda x: int(x, 0), - help="Width in pixels. Defaults to %r." % WIDTH) + help="Width in pixels. In subplots this instead expresses a " + "ratio of the current grid. Defaults to %r." % WIDTH) parser.add_argument( '-H', '--height', type=lambda x: int(x, 0), - help="Height in pixels. Defaults to %r." % HEIGHT) + help="Height in pixels. In subplots this instead expresses a " + "ratio of the current grid. Defaults to %r." % HEIGHT) + parser.add_argument( + '--wpad', + type=float, + help="Width padding in pt. Defaults to %r." % ( + 72*plt.rcParams["figure.constrained_layout.w_pad"])), + parser.add_argument( + '--hpad', + type=float, + help="Height padding in pt. Defaults to %r." % ( + 72*plt.rcParams["figure.constrained_layout.h_pad"])), + parser.add_argument( + '--wspace', + type=float, + help="Width spacing between plots as a ratio of the grid. " + "Defaults to %r." % 0) + parser.add_argument( + '--hspace', + type=float, + help="Height spacing between plots as a ratio of the grid. " + "Defaults to %r." % 0) parser.add_argument( '-X', '--xlim', type=lambda x: tuple(