diff --git a/AGENTS.md b/AGENTS.md index 61b7691..1a846fe 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -54,9 +54,12 @@ Do not commit or push if either fails. Fix issues first. Place them with `add_axes_inches`, `legend_inches` and `suptitle_inches` from `mplplots.utils`, not with `tight_layout`, `subplots_adjust` or a tight bounding box. - Keep the style's text: pass no `fontsize` or `fontweight`. +- Write variables and formulas in labels, titles and legends as math text (`"$x$"`, + `r"$\sin(x)$"`), never as upright text (`"x"`, `"sin(x)"`). The style sets math variables in + Arial Italic and function names, numbers and operators upright. - Render every figure in three formats with `save()` from `examples/_common.py`: PNG at 600 dpi, SVG and PDF, side by side in `examples/`, uncropped and without timestamps, so rerunning an unchanged script reproduces the files byte for byte. -- Rendering needs Arial installed; the scripts exit without it. After changing the style or a +- Rendering needs Arial Regular and Italic installed; the scripts exit without them. After changing the style or a script, rerun both scripts and commit all three formats. In `README.md`, show each image at 100 pixels per inch of figure width. diff --git a/README.md b/README.md index 6b0a7e1..1fc2a79 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,10 @@ plt.style.use("mplplots.styles.GraphPadPrism") x = np.linspace(0, 10, 50) fig, ax = plt.subplots() -ax.plot(x, np.sin(x), color=BLUES[4], label="sin(x)") -ax.plot(x, np.cos(x), color=REDS[4], label="cos(x)") -ax.set_xlabel("x") -ax.set_ylabel("y") +ax.plot(x, np.sin(x), color=BLUES[4], label=r"$\sin(x)$") +ax.plot(x, np.cos(x), color=REDS[4], label=r"$\cos(x)$") +ax.set_xlabel("$x$") +ax.set_ylabel("$y$") ax.legend() auto_ticks(ax, left=0, right=10) fig.savefig("figure.png") @@ -106,7 +106,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. 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. | +| 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: variables such as `$x$` in Arial Italic, and function names such as `$\sin$`, numbers, and operators upright. | | 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 pt axis lines, with the top and right spines removed. Autoscaled limits are rounded out to tick values. | | Ticks | 1 pt major and minor ticks, with minor ticks shown on both axes. | @@ -128,14 +128,18 @@ plotting: ```python from mplplots.utils import add_custom_fonts -add_custom_fonts("/path/to/Arial.ttf", "/path/to/Arial Bold.ttf") +add_custom_fonts("/path/to/Arial.ttf", "/path/to/Arial Italic.ttf", "/path/to/Arial Bold.ttf") ``` -`add_custom_fonts` registers the files for the current Python session only. The style itself uses -the regular face. Register the bold face as well for bold text you add, such as `fontweight="bold"` -or `$\mathbf{...}$`. Matplotlib caches its font list. If you install Arial system-wide after -Matplotlib has already run, delete the `fontlist-*.json` file in `matplotlib.get_cachedir()` so the -list is rebuilt. +`add_custom_fonts` registers the files for the current Python session only. The style uses the +regular face for text and the italic face for variables in math text. Register the bold face as +well for bold text you add, such as `fontweight="bold"` or `$\mathbf{...}$`. Matplotlib caches its +font list. If you install Arial system-wide after Matplotlib has already run, delete the +`fontlist-*.json` file in `matplotlib.get_cachedir()` so the list is rebuilt. + +Write variables and formulas in labels, titles, and legends as math text, so they are set in +italic like an equation: `ax.set_xlabel("$x$")` and `label=r"$\sin(x)$"`, not `"x"` and `"sin(x)"`. +Math text uses Matplotlib's built-in TeX parser, so it needs no LaTeX installation. ## Color palettes @@ -295,11 +299,11 @@ plt.style.use("mplplots.styles.GraphPadPrism") x = np.linspace(0, 10, 50) fig = plt.figure(figsize=(3, 2.25)) # the saved file is 3 × 2.25 in ax = add_axes_inches(fig, left=0.5, bottom=0.4, width=2.25, height=1.5) # 2.25 in by 1.5 in axes -ax.plot(x, np.sin(x), label="sin(x)") -ax.plot(x, np.cos(x), label="cos(x)") +ax.plot(x, np.sin(x), label=r"$\sin(x)$") +ax.plot(x, np.cos(x), label=r"$\cos(x)$") auto_ticks(ax, left=0, right=10) -ax.set_xlabel("x") -ax.set_ylabel("y") +ax.set_xlabel("$x$") +ax.set_ylabel("$y$") legend_inches(ax, 2.75, 1.9, loc="upper right", ncols=2) # upper-right corner at (2.75, 1.9) in fig.savefig("figure.pdf") ``` @@ -384,8 +388,9 @@ Every image uses the GraphPadPrism style, except the Matplotlib-defaults compari this README. Each figure is laid out in round inches (its size, the position and size of each axes, and the anchors of legends and titles), and the files are saved uncropped, so they keep that size. Apart from the comparison at the top, the README shows the images at 100 pixels per inch, so the -8 pt text appears the same size in each. The scripts need Arial installed and exit with an error -without it, instead of writing images in a fallback font. +8 pt text appears the same size in each. Variables in labels and legends are math text, such as +`$x$` and `$\sin(x)$`. The scripts need Arial Regular and Italic installed and exit with an error +without them, instead of writing images in a fallback font. ## Development diff --git a/examples/_common.py b/examples/_common.py index 36ab412..28fcaad 100644 --- a/examples/_common.py +++ b/examples/_common.py @@ -33,14 +33,17 @@ def require_arial() -> None: - """Exit unless Arial is installed. + """Exit unless Arial Regular and Arial Italic are installed. Without Arial the style silently falls back to a look-alike font (Liberation Sans, - ...), so the README images are never written in anything but Arial. + ...), so the README images are never written in anything but Arial. Text uses the + regular face and variables in math text (``$x$``) the italic one. """ faces = {(f.name, f.style, f.weight) for f in font_manager.fontManager.ttflist} - if ("Arial", "normal", 400) not in faces: - sys.exit("Arial is not installed; install it to regenerate the images.") + if not {("Arial", "normal", 400), ("Arial", "italic", 400)} <= faces: + sys.exit( + "Arial (regular and italic) is not installed; install it to regenerate the images." + ) def save(fig: Figure, name: str) -> None: diff --git a/examples/default_style.pdf b/examples/default_style.pdf index 5876611..0e55c2c 100644 Binary files a/examples/default_style.pdf and b/examples/default_style.pdf differ diff --git a/examples/default_style.png b/examples/default_style.png index b8f0aff..29d522f 100644 Binary files a/examples/default_style.png and b/examples/default_style.png differ diff --git a/examples/default_style.svg b/examples/default_style.svg index 46ff910..0a8d8f1 100644 --- a/examples/default_style.svg +++ b/examples/default_style.svg @@ -281,26 +281,26 @@ z - - + + - - + @@ -512,28 +512,28 @@ z - - + + - - + @@ -956,26 +956,26 @@ z - - - + + - - - - - - + + + + + + @@ -1069,7 +1069,7 @@ L 86.6 292.681656 " style="fill: none; stroke: #f34a36; stroke-width: 2; stroke-linecap: square"/> - + - - - - - - + + + + + + diff --git a/examples/graphpadprism_errorbar.pdf b/examples/graphpadprism_errorbar.pdf index 4f71fe6..8a72703 100644 Binary files a/examples/graphpadprism_errorbar.pdf and b/examples/graphpadprism_errorbar.pdf differ diff --git a/examples/graphpadprism_errorbar.png b/examples/graphpadprism_errorbar.png index d3a177d..3d58525 100644 Binary files a/examples/graphpadprism_errorbar.png and b/examples/graphpadprism_errorbar.png differ diff --git a/examples/graphpadprism_errorbar.svg b/examples/graphpadprism_errorbar.svg index 3174e71..95e0bd5 100644 --- a/examples/graphpadprism_errorbar.svg +++ b/examples/graphpadprism_errorbar.svg @@ -328,26 +328,27 @@ L 0 2 - - + + - @@ -524,15 +525,15 @@ L 416 0 z " transform="scale(0.015625)"/> - - - - - - - - - + + + + + + + + + @@ -749,33 +750,33 @@ L -2 0 - - + + - - - - - - - - - - + + + + + + + + + diff --git a/examples/graphpadprism_style.pdf b/examples/graphpadprism_style.pdf index 37942e5..1b6026c 100644 Binary files a/examples/graphpadprism_style.pdf and b/examples/graphpadprism_style.pdf differ diff --git a/examples/graphpadprism_style.png b/examples/graphpadprism_style.png index bec0ff1..923cf8d 100644 Binary files a/examples/graphpadprism_style.png and b/examples/graphpadprism_style.png differ diff --git a/examples/graphpadprism_style.svg b/examples/graphpadprism_style.svg index fd30335..fe10ec6 100644 --- a/examples/graphpadprism_style.svg +++ b/examples/graphpadprism_style.svg @@ -328,29 +328,28 @@ L 0 2 - + - - + @@ -557,32 +556,29 @@ L -2 0 - - + + - - + @@ -1133,14 +1129,14 @@ z - - - + + - - - - - - + + + + + + - - - - - - - - - + + + + + + + + diff --git a/examples/styles_demo.py b/examples/styles_demo.py index b31f291..9b98908 100644 --- a/examples/styles_demo.py +++ b/examples/styles_demo.py @@ -17,11 +17,11 @@ # 1. Default matplotlib vs GraphPadPrism # --------------------------------------------------------------------------- fig, ax = plt.subplots() -ax.plot(x, np.sin(x), color=BLUES[4], linewidth=2, label="sin(x)") -ax.plot(x, np.cos(x), color=REDS[4], linewidth=2, label="cos(x)") +ax.plot(x, np.sin(x), color=BLUES[4], linewidth=2, label=r"$\sin(x)$") +ax.plot(x, np.cos(x), color=REDS[4], linewidth=2, label=r"$\cos(x)$") ax.set_title("Default matplotlib") -ax.set_xlabel("x") -ax.set_ylabel("y") +ax.set_xlabel("$x$") +ax.set_ylabel("$y$") ax.legend() save(fig, "default_style") @@ -29,12 +29,12 @@ fig = plt.figure(figsize=(3, 2.25)) ax = add_axes_inches(fig, 0.5, 0.4, 2.25, 1.5) -ax.plot(x, np.sin(x), color=BLUES[4], label="sin(x)") -ax.plot(x, np.cos(x), color=REDS[4], label="cos(x)") +ax.plot(x, np.sin(x), color=BLUES[4], label=r"$\sin(x)$") +ax.plot(x, np.cos(x), color=REDS[4], label=r"$\cos(x)$") auto_ticks(ax, left=0, right=10) ax.set_title("GraphPadPrism + auto_ticks") -ax.set_xlabel("x") -ax.set_ylabel("y") +ax.set_xlabel("$x$") +ax.set_ylabel("$y$") legend_inches(ax, 2.75, 1.9, loc="upper right", ncols=2) save(fig, "graphpadprism_style") @@ -112,8 +112,8 @@ label=f"Group {i + 1}", ) auto_ticks(ax) -ax.set_xlabel("X measure") -ax.set_ylabel("Y measure") +ax.set_xlabel("$X$ measure") +ax.set_ylabel("$Y$ measure") ax.set_title("CORAL palette — error bars") legend_inches(ax, 3.1, 1.9) save(fig, "graphpadprism_errorbar") diff --git a/tests/test_styles.py b/tests/test_styles.py index d16c47d..c208ef8 100644 --- a/tests/test_styles.py +++ b/tests/test_styles.py @@ -215,6 +215,13 @@ def test_math_uses_text_font(self) -> None: math_font = fm.get_font(fm.findfont(matplotlib.rcParams[key])) assert math_font.family_name == text_font.family_name, key + def test_math_variables_are_italic_and_functions_upright(self) -> None: + with plt.style.context(GRAPHPAD_STYLE): + variable = fm.get_font(fm.findfont(matplotlib.rcParams["mathtext.it"])) + function = fm.get_font(fm.findfont(matplotlib.rcParams["mathtext.rm"])) + assert any(word in variable.style_name for word in ("Italic", "Oblique")) + assert not any(word in function.style_name for word in ("Italic", "Oblique")) + def test_renders_without_font_warnings(self) -> None: result = subprocess.run( [sys.executable, "-c", RENDER_SCRIPT, GRAPHPAD_STYLE],