From c84d9183395f2da1fe10434ffd88a5739a101380 Mon Sep 17 00:00:00 2001 From: FridrichMethod Date: Mon, 16 Nov 2026 18:05:59 -0800 Subject: [PATCH] feat(style): stop cropping saved figures Drop savefig.bbox: tight, so a saved file keeps the figure's size, which round-inch layouts rely on. Pass bbox_inches="tight" to savefig to crop. --- AGENTS.md | 3 ++- README.md | 2 +- src/mplplots/styles/GraphPadPrism.mplstyle | 4 ++-- tests/test_styles.py | 12 ++++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 4407b94..22ab492 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -39,7 +39,8 @@ Do not commit or push if either fails. Fix issues first. tick labels, axis labels, axes and figure titles, figure-level axis labels, and the legend and its title. Do not give any of them another size or weight. - It saves at 600 dpi (`savefig.dpi`), embeds TrueType fonts in PDF and PostScript - (`pdf.fonttype` and `ps.fonttype` 42), stores SVG text as paths, and sets no figure size. + (`pdf.fonttype` and `ps.fonttype` 42), stores SVG text as paths, sets no figure size, and does + not crop saved figures (no `savefig.bbox`), so a saved file has exactly the figure's size. - A change to the style goes together with the "What the style changes" table in `README.md` and the assertions in `tests/test_styles.py`. diff --git a/README.md b/README.md index 9e2ac40..fafd5ef 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ the `stylelib/` folder of your Matplotlib config directory (`matplotlib.get_conf | Area | Settings | | --- | --- | -| Figure | No figure size is set. Saved at 600 dpi and shown on screen at 300 dpi. `savefig` crops to a tight bounding box. | +| Figure | No figure size is set. Saved at 600 dpi and shown on screen at 300 dpi. Saved files are not cropped, so they keep the figure's size; pass `bbox_inches="tight"` to `savefig` to crop to the drawn content. | | Text | Arial Regular 8 pt (see [Fonts](#fonts)) for every text element: tick labels, axis labels, axes and figure titles, and the legend and its title. Math text (`$...$`) uses the same font. | | Vector output | PDF and PostScript embed fonts as TrueType (Type 42), so text stays editable and passes journal checks that reject Type 3 fonts. SVG stores glyphs as paths. | | Axes | 1.5 pt axis lines, with the top and right spines removed. Autoscaled limits are rounded out to tick values. | diff --git a/src/mplplots/styles/GraphPadPrism.mplstyle b/src/mplplots/styles/GraphPadPrism.mplstyle index 1905b73..cc69a48 100644 --- a/src/mplplots/styles/GraphPadPrism.mplstyle +++ b/src/mplplots/styles/GraphPadPrism.mplstyle @@ -12,7 +12,8 @@ axes.titleweight: normal errorbar.capsize: 3 -# Screen resolution; saved files use savefig.dpi. The style sets no figure size. +# Screen resolution; saved files use savefig.dpi. The style sets no figure size and does not +# crop saved figures (no savefig.bbox), so a saved file has exactly the figure's size. figure.dpi: 300 figure.frameon: False figure.labelsize: 8 @@ -55,7 +56,6 @@ mathtext.sf: sans pdf.fonttype: 42 ps.fonttype: 42 -savefig.bbox: tight savefig.dpi: 600 # SVG stores glyphs as paths, so it looks the same on machines without Arial. diff --git a/tests/test_styles.py b/tests/test_styles.py index 14cc0d3..f9bd35b 100644 --- a/tests/test_styles.py +++ b/tests/test_styles.py @@ -95,6 +95,18 @@ def test_style_sets_dpi(self) -> None: assert matplotlib.rcParams["figure.dpi"] == 300 assert matplotlib.rcParams["savefig.dpi"] == 600 + def test_saved_figure_keeps_its_size(self) -> None: + # No tight bounding box: the file is the whole figure, so it keeps the figure's size. + with plt.style.context(GRAPHPAD_STYLE): + fig, ax = plt.subplots(figsize=(2, 1.5)) + ax.plot([1, 2], [1, 2]) + buffer = io.BytesIO() + fig.savefig(buffer, format="png", dpi=100) + plt.close(fig) + buffer.seek(0) + height, width = plt.imread(buffer).shape[:2] + assert (width, height) == (200, 150) + def test_style_sets_no_figure_size(self) -> None: with plt.rc_context({"figure.figsize": (1.5, 2.5)}), plt.style.context(GRAPHPAD_STYLE): assert list(matplotlib.rcParams["figure.figsize"]) == [1.5, 2.5]