Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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. |
Expand All @@ -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

Expand Down Expand Up @@ -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")
```
Expand Down Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions examples/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Binary file modified examples/default_style.pdf
Binary file not shown.
Binary file modified examples/default_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 58 additions & 58 deletions examples/default_style.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/graphpadprism_errorbar.pdf
Binary file not shown.
Binary file modified examples/graphpadprism_errorbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading