diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a0e040b..d29b9d5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,8 +2,11 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: + # The example SVGs are written by Matplotlib and must stay byte for byte as rendered. - id: trailing-whitespace + exclude: ^examples/.*\.svg$ - id: end-of-file-fixer + exclude: ^examples/.*\.svg$ - id: check-yaml - id: check-toml - id: check-added-large-files diff --git a/AGENTS.md b/AGENTS.md index 7842624..4407b94 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,3 +42,19 @@ Do not commit or push if either fails. Fix issues first. (`pdf.fonttype` and `ps.fonttype` 42), stores SVG text as paths, and sets no figure 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`. + +## Rendering Figures + +- The README images come from `examples/styles_demo.py` and `examples/palettes_demo.py`. All of + them use the GraphPadPrism style except `default_style`, which shows Matplotlib's defaults. +- Lay figures out in round inches: the figure size, each axes' position and size (the axis + lengths), and the anchors of legends and figure titles are multiples of 0.1, 0.25, 0.5 or 1 in. + Place them with `add_axes`, `legend` and `suptitle` from `examples/_common.py`, not with + `tight_layout`, `subplots_adjust` or a tight bounding box. +- Keep the style's text: pass no `fontsize` or `fontweight`. +- 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 + 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 7a400df..9e2ac40 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ palettes, and a few helpers for publication-quality scientific figures. Sine and cosine plotted with Matplotlib's default style - The same plot with the GraphPad Prism style: bold text, no top or right spines, minor ticks + The same plot with the GraphPad Prism style: Arial 8 pt text, no top or right spines, minor ticks @@ -154,7 +154,7 @@ slice it and pass it anywhere Matplotlib or seaborn accepts colors.
Show all 25 palettes -Swatches of all 25 hand-picked palettes +Swatches of all 25 hand-picked palettes
@@ -162,7 +162,7 @@ The colormap families `BLUES`, `GREENS`, `REDS`, `PURPLES`, `ORANGES`, and `GREY shades running from light to dark. Their colors are sampled from the matching Matplotlib colormap between 0.3 and 0.8 (0.25 and 0.75 for `GREYS`), which skips near-white and near-black. -Swatches of the colormap-derived color families +Swatches of the colormap-derived color families ### Using a palette @@ -227,7 +227,7 @@ get_colors("viridis", 5, start=0.8, end=0.2) # or sample in reverse order `n` must be at least 1, and `start` and `end` must be between 0 and 1. Otherwise, and for an unknown colormap name, `get_colors` raises `ValueError`. -Bar charts colored with get_colors from coolwarm, viridis, and YlGnBu +Bar charts colored with get_colors from coolwarm, viridis, and YlGnBu ## Ticks: `auto_ticks` @@ -281,16 +281,16 @@ Generated by [`examples/styles_demo.py`](examples/styles_demo.py): - - + + - - + + @@ -303,42 +303,43 @@ palette swatches and the `get_colors` figure above:
Bar chartMulti-line plotBar chartMulti-line plot
Bar chart colored with BLUES[:5] Phase-shifted waves in OCEAN
Grouped bar chartError bar plotGrouped bar chartError bar plot
Grouped bar chart in AUTUMN
- - + + - + - - + + - + - - + - - +
Multi-line plotScatter plotsMulti-line plotHorizontal bar chart
All 11 CUSTOM colors as linesScatter plots in OCEAN, ROSE, and FORESTHorizontal bar chart in DUSK
Stacked area chartHorizontal bar chartStacked area chartPie chart
Stacked area chart in SPRINGHorizontal bar chart in DUSKPie chart in CANDY
Pie chartScatter plots
Pie chart in CANDYScatter plots in OCEAN, ROSE, and FOREST
-To regenerate the images, run the scripts from any directory. The images are written to -`examples/`, next to the scripts: +To regenerate the images, run the scripts from any directory. Each figure is written to +`examples/`, next to the scripts, as PNG (600 dpi), SVG, and PDF: ```bash python examples/styles_demo.py python examples/palettes_demo.py ``` -All text in the images is set in Arial, except the Matplotlib-defaults comparison at the top of -this README. The scripts need Arial (regular and bold) installed and exit with an error without it, -instead of writing images in a fallback font. +Every image uses the GraphPadPrism style, except the Matplotlib-defaults comparison at the top of +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. ## Development diff --git a/examples/_common.py b/examples/_common.py new file mode 100644 index 0000000..5b930cd --- /dev/null +++ b/examples/_common.py @@ -0,0 +1,119 @@ +"""Shared setup for the example scripts: Arial check, round-inch layout and saving. + +Every figure is laid out in inches from its lower-left corner. The figure size, the +position and size of each axes, and the anchors of legends and figure titles are round +values: multiples of 0.1, 0.25, 0.5 or 1 in. Figures are saved uncropped, so the files +keep that size, as PNG (600 dpi), SVG and PDF. +""" + +from __future__ import annotations + +import sys +from pathlib import Path +from typing import TYPE_CHECKING, Any, Literal + +import matplotlib.pyplot as plt +from matplotlib import font_manager + +if TYPE_CHECKING: + from matplotlib.axes import Axes + from matplotlib.figure import Figure + from matplotlib.legend import Legend + from matplotlib.text import Text + from matplotlib.typing import RcKeyType # Matplotlib 3.10+ + +STYLE = "mplplots.styles.GraphPadPrism" +OUT = Path(__file__).resolve().parent +FORMATS = ("png", "svg", "pdf") +DPI = 600 + +# Without a fixed date and SVG id salt, every run would write different SVG and PDF files +# for the same figure. +_METADATA: dict[str, dict[str, Any]] = {"svg": {"Date": None}, "pdf": {"CreationDate": None}} +_SAVE_RC: dict[RcKeyType, Any] = {"savefig.bbox": "standard", "svg.hashsalt": "mplplots"} + + +def require_arial() -> None: + """Exit unless Arial is 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. + """ + 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.") + + +def add_axes(fig: Figure, left: float, bottom: float, width: float, height: float) -> Axes: + """Add axes of width x height inches whose lower-left corner is at (left, bottom). + + Args: + fig: The figure to add the axes to. + left: Distance from the figure's left edge to the axes, in inches. + bottom: Distance from the figure's bottom edge to the axes, in inches. + width: Width of the axes (the x-axis length), in inches. + height: Height of the axes (the y-axis length), in inches. + + Returns: + The new axes. + """ + fig_width, fig_height = fig.get_size_inches() + rect = (left / fig_width, bottom / fig_height, width / fig_width, height / fig_height) + return fig.add_axes(rect) + + +def legend( + fig: Figure, + ax: Axes, + x: float, + y: float, + loc: Literal["upper left", "upper right", "lower left", "lower right"] = "upper left", + **kwargs: Any, +) -> Legend: + """Add a legend for ax with its loc corner at (x, y) inches on the figure. + + Args: + fig: The figure that holds ax. + ax: The axes whose labelled artists the legend lists. + x: Distance from the figure's left edge to the legend's loc corner, in inches. + y: Distance from the figure's bottom edge to the legend's loc corner, in inches. + loc: Which corner of the legend sits at (x, y). + **kwargs: Passed on to ``ax.legend``, e.g. ``ncols``. + + Returns: + The new legend. + """ + return ax.legend( + loc=loc, + bbox_to_anchor=(x, y), + bbox_transform=fig.dpi_scale_trans, + borderaxespad=0, + **kwargs, + ) + + +def suptitle(fig: Figure, text: str, top: float = 0.1) -> Text: + """Center a figure title with its top edge top inches below the figure's top edge. + + Args: + fig: The figure to title. + text: The title. + top: Distance from the figure's top edge to the title's top edge, in inches. + + Returns: + The title text. + """ + return fig.suptitle(text, y=1 - top / fig.get_size_inches()[1], va="top") + + +def save(fig: Figure, name: str) -> None: + """Save fig uncropped as ``examples/.png`` (600 dpi), ``.svg`` and ``.pdf``. + + Args: + fig: The figure to save; it is closed afterwards. + name: The file name without extension. + """ + with plt.rc_context(_SAVE_RC): + for fmt in FORMATS: + fig.savefig(OUT / f"{name}.{fmt}", dpi=DPI, metadata=_METADATA.get(fmt)) + plt.close(fig) diff --git a/examples/all_palettes.pdf b/examples/all_palettes.pdf new file mode 100644 index 0000000..283f8e9 Binary files /dev/null and b/examples/all_palettes.pdf differ diff --git a/examples/all_palettes.png b/examples/all_palettes.png index fde2e71..e4225fd 100644 Binary files a/examples/all_palettes.png and b/examples/all_palettes.png differ diff --git a/examples/all_palettes.svg b/examples/all_palettes.svg new file mode 100644 index 0000000..e62f904 --- /dev/null +++ b/examples/all_palettes.svg @@ -0,0 +1,3496 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/candy_pie.pdf b/examples/candy_pie.pdf new file mode 100644 index 0000000..a07bbaa Binary files /dev/null and b/examples/candy_pie.pdf differ diff --git a/examples/candy_pie.png b/examples/candy_pie.png index 621b4d9..42d6f4e 100644 Binary files a/examples/candy_pie.png and b/examples/candy_pie.png differ diff --git a/examples/candy_pie.svg b/examples/candy_pie.svg new file mode 100644 index 0000000..828b6d1 --- /dev/null +++ b/examples/candy_pie.svg @@ -0,0 +1,448 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/color_families.pdf b/examples/color_families.pdf new file mode 100644 index 0000000..c886383 Binary files /dev/null and b/examples/color_families.pdf differ diff --git a/examples/color_families.png b/examples/color_families.png index b06f547..b0f83dc 100644 Binary files a/examples/color_families.png and b/examples/color_families.png differ diff --git a/examples/color_families.svg b/examples/color_families.svg new file mode 100644 index 0000000..d9f848d --- /dev/null +++ b/examples/color_families.svg @@ -0,0 +1,1094 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/custom_multiline.pdf b/examples/custom_multiline.pdf new file mode 100644 index 0000000..a2e2a2f Binary files /dev/null and b/examples/custom_multiline.pdf differ diff --git a/examples/custom_multiline.png b/examples/custom_multiline.png index 7aa37c1..404d700 100644 Binary files a/examples/custom_multiline.png and b/examples/custom_multiline.png differ diff --git a/examples/custom_multiline.svg b/examples/custom_multiline.svg new file mode 100644 index 0000000..ec45620 --- /dev/null +++ b/examples/custom_multiline.svg @@ -0,0 +1,2431 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/default_style.pdf b/examples/default_style.pdf new file mode 100644 index 0000000..5876611 Binary files /dev/null and b/examples/default_style.pdf differ diff --git a/examples/default_style.png b/examples/default_style.png index 34d8914..b8f0aff 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 new file mode 100644 index 0000000..46ff910 --- /dev/null +++ b/examples/default_style.svg @@ -0,0 +1,1113 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/dusk_hbar.pdf b/examples/dusk_hbar.pdf new file mode 100644 index 0000000..c4c85c0 Binary files /dev/null and b/examples/dusk_hbar.pdf differ diff --git a/examples/dusk_hbar.png b/examples/dusk_hbar.png index ef725df..a966faa 100644 Binary files a/examples/dusk_hbar.png and b/examples/dusk_hbar.png differ diff --git a/examples/dusk_hbar.svg b/examples/dusk_hbar.svg new file mode 100644 index 0000000..d064c75 --- /dev/null +++ b/examples/dusk_hbar.svg @@ -0,0 +1,1294 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/get_colors_demo.pdf b/examples/get_colors_demo.pdf new file mode 100644 index 0000000..0967e50 Binary files /dev/null and b/examples/get_colors_demo.pdf differ diff --git a/examples/get_colors_demo.png b/examples/get_colors_demo.png index 0defed6..8abb1b8 100644 Binary files a/examples/get_colors_demo.png and b/examples/get_colors_demo.png differ diff --git a/examples/get_colors_demo.svg b/examples/get_colors_demo.svg new file mode 100644 index 0000000..787e52e --- /dev/null +++ b/examples/get_colors_demo.svg @@ -0,0 +1,1336 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/graphpadprism_bar.pdf b/examples/graphpadprism_bar.pdf new file mode 100644 index 0000000..d7406de Binary files /dev/null and b/examples/graphpadprism_bar.pdf differ diff --git a/examples/graphpadprism_bar.png b/examples/graphpadprism_bar.png index 3597361..606abfa 100644 Binary files a/examples/graphpadprism_bar.png and b/examples/graphpadprism_bar.png differ diff --git a/examples/graphpadprism_bar.svg b/examples/graphpadprism_bar.svg new file mode 100644 index 0000000..3b54e02 --- /dev/null +++ b/examples/graphpadprism_bar.svg @@ -0,0 +1,930 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/graphpadprism_errorbar.pdf b/examples/graphpadprism_errorbar.pdf new file mode 100644 index 0000000..68dc57c Binary files /dev/null and b/examples/graphpadprism_errorbar.pdf differ diff --git a/examples/graphpadprism_errorbar.png b/examples/graphpadprism_errorbar.png index 482f5cd..46ca69e 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 new file mode 100644 index 0000000..04264c2 --- /dev/null +++ b/examples/graphpadprism_errorbar.svg @@ -0,0 +1,1620 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/graphpadprism_grouped_bar.pdf b/examples/graphpadprism_grouped_bar.pdf new file mode 100644 index 0000000..62e0e97 Binary files /dev/null and b/examples/graphpadprism_grouped_bar.pdf differ diff --git a/examples/graphpadprism_grouped_bar.png b/examples/graphpadprism_grouped_bar.png index 9743028..f369c52 100644 Binary files a/examples/graphpadprism_grouped_bar.png and b/examples/graphpadprism_grouped_bar.png differ diff --git a/examples/graphpadprism_grouped_bar.svg b/examples/graphpadprism_grouped_bar.svg new file mode 100644 index 0000000..c38c57e --- /dev/null +++ b/examples/graphpadprism_grouped_bar.svg @@ -0,0 +1,1296 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/graphpadprism_ocean.pdf b/examples/graphpadprism_ocean.pdf new file mode 100644 index 0000000..fbec780 Binary files /dev/null and b/examples/graphpadprism_ocean.pdf differ diff --git a/examples/graphpadprism_ocean.png b/examples/graphpadprism_ocean.png index 9ed4a96..9ab6496 100644 Binary files a/examples/graphpadprism_ocean.png and b/examples/graphpadprism_ocean.png differ diff --git a/examples/graphpadprism_ocean.svg b/examples/graphpadprism_ocean.svg new file mode 100644 index 0000000..a81ec62 --- /dev/null +++ b/examples/graphpadprism_ocean.svg @@ -0,0 +1,1576 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/graphpadprism_style.pdf b/examples/graphpadprism_style.pdf new file mode 100644 index 0000000..1106722 Binary files /dev/null and b/examples/graphpadprism_style.pdf differ diff --git a/examples/graphpadprism_style.png b/examples/graphpadprism_style.png index e30d073..1eadc25 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 new file mode 100644 index 0000000..f06f86b --- /dev/null +++ b/examples/graphpadprism_style.svg @@ -0,0 +1,1226 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/palettes_demo.py b/examples/palettes_demo.py index 8d23c3d..6486243 100644 --- a/examples/palettes_demo.py +++ b/examples/palettes_demo.py @@ -1,11 +1,11 @@ -"""Demonstrate mplplots color palettes.""" +"""Demonstrate mplplots color palettes with the GraphPadPrism style.""" -import sys -from pathlib import Path +from collections.abc import Mapping, Sequence import matplotlib.pyplot as plt import numpy as np -from matplotlib import font_manager +from _common import STYLE, add_axes, legend, require_arial, save, suptitle +from matplotlib.figure import Figure import mplplots.palettes as palettes from mplplots.palettes import ( @@ -17,60 +17,78 @@ REDS, get_colors, ) +from mplplots.utils import auto_ticks + +require_arial() +plt.style.use(STYLE) + + +def swatches( + fig: Figure, + rows: Mapping[str, Sequence[str]], + left: float, + bottom: float, + cell: tuple[float, float], + gap: float, +) -> None: + """Draw one labelled row of color swatches per palette, top to bottom. + + Args: + fig: The figure to draw on. + rows: Palette name -> colors, in the order the rows are drawn from the top. + left: Distance from the figure's left edge to the swatches, in inches. + bottom: Distance from the figure's bottom edge to the lowest row, in inches. + cell: Width and height of one swatch, in inches. + gap: Vertical space between rows, in inches. + """ + cell_width, cell_height = cell + for row, (name, colors) in enumerate(reversed(list(rows.items()))): + ax = add_axes( + fig, left, bottom + row * (cell_height + gap), len(colors) * cell_width, cell_height + ) + for i, c in enumerate(colors): + ax.barh(0, 1, left=i, height=1, color=c, edgecolor="none") + ax.set_xlim(0, len(colors)) + ax.set_ylim(-0.5, 0.5) + ax.set_xticks([]) + ax.set_yticks([]) + ax.set_ylabel(name, rotation=0, ha="right", va="center") + ax.set_frame_on(False) -OUT = Path(__file__).resolve().parent - -# Matplotlib would silently fall back to DejaVu Sans, so refuse to write the README images in -# anything but Arial. -ARIAL_FACES = {("Arial", "normal", 400), ("Arial", "normal", 700)} -if not {(f.name, f.style, f.weight) for f in font_manager.fontManager.ttflist} >= ARIAL_FACES: - sys.exit("Arial (regular and bold) is not installed; install it to regenerate the images.") - -# Arial text, as in the GraphPadPrism style; everything else keeps Matplotlib's defaults. -plt.rcParams["font.family"] = "Arial" # --------------------------------------------------------------------------- # 1. Multi-line plot using CUSTOM palette # --------------------------------------------------------------------------- x = np.linspace(0, 2 * np.pi, 100) -fig, ax = plt.subplots(figsize=(8, 4)) +fig = plt.figure(figsize=(4.5, 2.75)) +ax = add_axes(fig, 0.5, 0.4, 3, 2) for i, color in enumerate(CUSTOM): - ax.plot(x, np.sin(x + i * 0.3), color=color, linewidth=2, label=f"Line {i}") + ax.plot(x, np.sin(x + i * 0.3), color=color, label=f"Line {i}") +auto_ticks(ax, left=0) ax.set_title("CUSTOM palette — multi-line plot") -ax.legend(fontsize=7, ncol=3) -fig.savefig(f"{OUT}/custom_multiline.png", dpi=150, bbox_inches="tight") -plt.close(fig) +legend(fig, ax, 3.6, 2.4) +save(fig, "custom_multiline") # --------------------------------------------------------------------------- # 2. Colormap-derived family swatches # --------------------------------------------------------------------------- families = {"BLUES": BLUES, "GREENS": GREENS, "REDS": REDS, "PURPLES": PURPLES, "ORANGES": ORANGES} -fig, axes = plt.subplots(len(families), 1, figsize=(8, 4)) -for ax, (name, colors) in zip(axes, families.items()): - for i, c in enumerate(colors): - ax.barh(0, 1, left=i, color=c, edgecolor="none") - ax.set_xlim(0, len(colors)) - ax.set_yticks([]) - ax.set_xticks([]) - ax.set_ylabel(name, rotation=0, ha="right", va="center", fontsize=10) - ax.set_frame_on(False) -fig.suptitle("Colormap-derived families", fontsize=14) -fig.tight_layout() -fig.savefig(f"{OUT}/color_families.png", dpi=150, bbox_inches="tight") -plt.close(fig) +fig = plt.figure(figsize=(4, 1.9)) +swatches(fig, families, left=0.75, bottom=0.1, cell=(0.4, 0.2), gap=0.1) +suptitle(fig, "Colormap-derived families") +save(fig, "color_families") # --------------------------------------------------------------------------- # 3. get_colors() with different colormaps # --------------------------------------------------------------------------- -fig, axes = plt.subplots(1, 3, figsize=(10, 4)) -for ax, cmap_name in zip(axes, ["coolwarm", "viridis", "YlGnBu"]): +fig = plt.figure(figsize=(6, 1.8)) +for i, cmap_name in enumerate(["coolwarm", "viridis", "YlGnBu"]): + ax = add_axes(fig, 0.4 + i * 2, 0.2, 1.5, 1.25) colors = get_colors(cmap_name, 6, 0.15, 0.85) ax.bar(range(len(colors)), [3 + i * 0.5 for i in range(len(colors))], color=colors) ax.set_title(f'get_colors("{cmap_name}", 6)') ax.set_xticks([]) -fig.tight_layout() -fig.savefig(f"{OUT}/get_colors_demo.png", dpi=150, bbox_inches="tight") -plt.close(fig) +save(fig, "get_colors_demo") # --------------------------------------------------------------------------- # 4. Full catalogue — all 25 custom-style palettes as swatches @@ -103,78 +121,75 @@ "POWDER", ] -fig, axes = plt.subplots(len(ALL_NAMES), 1, figsize=(12, 18)) -for ax, name in zip(axes, ALL_NAMES): - colors = getattr(palettes, name) - for i, c in enumerate(colors): - ax.barh(0, 1, left=i, color=c, edgecolor="none") - ax.set_xlim(0, len(colors)) - ax.set_yticks([]) - ax.set_xticks([]) - ax.set_ylabel(name, rotation=0, ha="right", va="center", fontsize=9, fontweight="bold") - ax.set_frame_on(False) -fig.suptitle("All 25 custom-style palettes", fontsize=14, fontweight="bold", y=0.995) -fig.tight_layout() -fig.savefig(f"{OUT}/all_palettes.png", dpi=150, bbox_inches="tight") -plt.close(fig) +fig = plt.figure(figsize=(4.25, 5.5)) +swatches( + fig, + {name: getattr(palettes, name) for name in ALL_NAMES}, + left=0.75, + bottom=0.1, + cell=(0.3, 0.15), + gap=0.05, +) +suptitle(fig, "All 25 custom-style palettes") +save(fig, "all_palettes") # --------------------------------------------------------------------------- # 5. Scatter plot — comparing three palette styles # --------------------------------------------------------------------------- rng = np.random.default_rng(42) -fig, axes = plt.subplots(1, 3, figsize=(14, 4)) -for ax, (name, pal_name) in zip(axes, [("OCEAN", "OCEAN"), ("ROSE", "ROSE"), ("FOREST", "FOREST")]): - pal = getattr(palettes, pal_name) - for i, c in enumerate(pal): - xs = rng.normal(i, 0.3, 20) - ys = rng.normal(i * 0.5, 0.4, 20) - ax.scatter(xs, ys, color=c, s=40, alpha=0.8, edgecolors="none") +fig = plt.figure(figsize=(5.5, 1.7)) +for i, name in enumerate(["OCEAN", "ROSE", "FOREST"]): + ax = add_axes(fig, 0.25 + i * 1.75, 0.1, 1.5, 1.25) + pal = getattr(palettes, name) + for j, c in enumerate(pal): + xs = rng.normal(j, 0.3, 20) + ys = rng.normal(j * 0.5, 0.4, 20) + ax.scatter(xs, ys, color=c, s=10, alpha=0.8, edgecolors="none") ax.set_title(f"{name} — scatter") ax.set_xticks([]) ax.set_yticks([]) -fig.tight_layout() -fig.savefig(f"{OUT}/scatter_comparison.png", dpi=150, bbox_inches="tight") -plt.close(fig) +save(fig, "scatter_comparison") # --------------------------------------------------------------------------- # 6. Area chart with SPRING palette # --------------------------------------------------------------------------- x = np.linspace(0, 4, 80) -fig, ax = plt.subplots(figsize=(8, 4)) +fig = plt.figure(figsize=(4.5, 2.5)) +ax = add_axes(fig, 0.5, 0.4, 3, 1.75) spring = palettes.SPRING base = np.zeros_like(x) for i in range(6): y = np.abs(np.sin(x + i * 0.5)) * (1 + 0.3 * i) ax.fill_between(x, base, base + y, color=spring[i], alpha=0.85, label=f"Layer {i}") base += y +auto_ticks(ax, left=0, right=4, bottom=0) ax.set_title("SPRING palette — stacked area chart") -ax.legend(fontsize=8, ncol=3) -fig.savefig(f"{OUT}/spring_area.png", dpi=150, bbox_inches="tight") -plt.close(fig) +legend(fig, ax, 3.6, 2.15) +save(fig, "spring_area") # --------------------------------------------------------------------------- # 7. Pie chart with CANDY palette # --------------------------------------------------------------------------- -fig, ax = plt.subplots(figsize=(5, 5)) +fig = plt.figure(figsize=(2.5, 2.5)) +ax = add_axes(fig, 0.25, 0.1, 2, 2) candy = palettes.CANDY sizes = [22, 18, 15, 13, 10, 8, 7, 4, 2, 1] ax.pie(sizes, colors=candy[:10], startangle=90, wedgeprops={"edgecolor": "white", "linewidth": 1.5}) ax.set_title("CANDY palette — pie chart") -fig.savefig(f"{OUT}/candy_pie.png", dpi=150, bbox_inches="tight") -plt.close(fig) +save(fig, "candy_pie") # --------------------------------------------------------------------------- # 8. Horizontal bar chart with DUSK palette # --------------------------------------------------------------------------- -fig, ax = plt.subplots(figsize=(7, 5)) +fig = plt.figure(figsize=(3.5, 3)) +ax = add_axes(fig, 0.6, 0.4, 2.6, 2.25) dusk = palettes.DUSK categories = [f"Item {chr(65 + i)}" for i in range(11)] values = [45, 38, 35, 30, 28, 24, 20, 18, 14, 10, 6] ax.barh(categories, values, color=dusk) ax.set_title("DUSK palette — horizontal bar chart") ax.invert_yaxis() -fig.tight_layout() -fig.savefig(f"{OUT}/dusk_hbar.png", dpi=150, bbox_inches="tight") -plt.close(fig) +auto_ticks(ax, left=0) +save(fig, "dusk_hbar") print("All palette examples saved to examples/") diff --git a/examples/scatter_comparison.pdf b/examples/scatter_comparison.pdf new file mode 100644 index 0000000..3b6a3f6 Binary files /dev/null and b/examples/scatter_comparison.pdf differ diff --git a/examples/scatter_comparison.png b/examples/scatter_comparison.png index 7986e28..2be4fd3 100644 Binary files a/examples/scatter_comparison.png and b/examples/scatter_comparison.png differ diff --git a/examples/scatter_comparison.svg b/examples/scatter_comparison.svg new file mode 100644 index 0000000..6a87c47 --- /dev/null +++ b/examples/scatter_comparison.svg @@ -0,0 +1,3011 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/spring_area.pdf b/examples/spring_area.pdf new file mode 100644 index 0000000..a24f4b3 Binary files /dev/null and b/examples/spring_area.pdf differ diff --git a/examples/spring_area.png b/examples/spring_area.png index f6025f5..e757f41 100644 Binary files a/examples/spring_area.png and b/examples/spring_area.png differ diff --git a/examples/spring_area.svg b/examples/spring_area.svg new file mode 100644 index 0000000..3d6b345 --- /dev/null +++ b/examples/spring_area.svg @@ -0,0 +1,2243 @@ + + + + + + + + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/styles_demo.py b/examples/styles_demo.py index 36afe59..bf86bc7 100644 --- a/examples/styles_demo.py +++ b/examples/styles_demo.py @@ -1,134 +1,121 @@ """Demonstrate mplplots styles with palettes and auto_ticks.""" -import sys -from pathlib import Path - import matplotlib.pyplot as plt import numpy as np -from matplotlib import font_manager, ticker +from _common import STYLE, add_axes, legend, require_arial, save +from matplotlib import ticker import mplplots.palettes as palettes from mplplots.palettes import BLUES, REDS from mplplots.utils import auto_ticks -STYLE = "mplplots.styles.GraphPadPrism" -OUT = Path(__file__).resolve().parent - -# Without Arial the style silently falls back to a look-alike font (Liberation Sans, ...), -# so refuse to write the README images in anything but Arial. -ARIAL_FACES = {("Arial", "normal", 400), ("Arial", "normal", 700)} -if not {(f.name, f.style, f.weight) for f in font_manager.fontManager.ttflist} >= ARIAL_FACES: - sys.exit("Arial (regular and bold) is not installed; install it to regenerate the images.") +require_arial() x = np.linspace(0, 10, 50) # --------------------------------------------------------------------------- # 1. Default matplotlib vs GraphPadPrism # --------------------------------------------------------------------------- -fig, ax = plt.subplots(figsize=(5, 4)) +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.set_title("Default matplotlib") ax.set_xlabel("x") ax.set_ylabel("y") ax.legend() -fig.savefig(f"{OUT}/default_style.png", dpi=150, bbox_inches="tight") -plt.close(fig) +save(fig, "default_style") + +plt.style.use(STYLE) -with plt.style.context(STYLE): - 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)") - auto_ticks(ax, left=0, right=10) - ax.set_title("GraphPadPrism + auto_ticks") - ax.set_xlabel("x") - ax.set_ylabel("y") - ax.legend() - fig.savefig(f"{OUT}/graphpadprism_style.png", dpi=150, bbox_inches="tight") - plt.close(fig) +fig = plt.figure(figsize=(3, 2.25)) +ax = add_axes(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)") +auto_ticks(ax, left=0, right=10) +ax.set_title("GraphPadPrism + auto_ticks") +ax.set_xlabel("x") +ax.set_ylabel("y") +legend(fig, ax, 2.75, 1.9, loc="upper right", ncols=2) +save(fig, "graphpadprism_style") # --------------------------------------------------------------------------- # 2. Bar chart with GraphPadPrism # --------------------------------------------------------------------------- -with plt.style.context(STYLE): - fig, ax = plt.subplots() - categories = ["A", "B", "C", "D", "E"] - values = [23, 45, 12, 67, 34] - ax.bar(categories, values, color=BLUES[:5]) - auto_ticks(ax, bottom=0, top=80) - ax.set_xlabel("Category") - ax.set_ylabel("Value") - ax.set_title("Bar chart") - fig.savefig(f"{OUT}/graphpadprism_bar.png", dpi=150, bbox_inches="tight") - plt.close(fig) +fig = plt.figure(figsize=(2.75, 2.25)) +ax = add_axes(fig, 0.5, 0.4, 2, 1.5) +categories = ["A", "B", "C", "D", "E"] +values = [23, 45, 12, 67, 34] +ax.bar(categories, values, color=BLUES[:5]) +auto_ticks(ax, bottom=0, top=80) +ax.set_xlabel("Category") +ax.set_ylabel("Value") +ax.set_title("Bar chart") +save(fig, "graphpadprism_bar") # --------------------------------------------------------------------------- # 3. Multi-line with GraphPadPrism + OCEAN palette # --------------------------------------------------------------------------- -with plt.style.context(STYLE): - fig, ax = plt.subplots(figsize=(6, 4)) - ocean = palettes.OCEAN - for i in range(6): - ax.plot(x, np.sin(x + i * 0.4) * (1 - i * 0.1), color=ocean[i], label=f"Wave {i}") - auto_ticks(ax, left=0, right=10) - ax.set_xlabel("Time") - ax.set_ylabel("Amplitude") - ax.set_title("OCEAN palette + GraphPadPrism") - ax.legend(fontsize=7, ncol=2) - fig.savefig(f"{OUT}/graphpadprism_ocean.png", dpi=150, bbox_inches="tight") - plt.close(fig) +fig = plt.figure(figsize=(4, 2.25)) +ax = add_axes(fig, 0.5, 0.4, 2.5, 1.5) +ocean = palettes.OCEAN +for i in range(6): + ax.plot(x, np.sin(x + i * 0.4) * (1 - i * 0.1), color=ocean[i], label=f"Wave {i}") +auto_ticks(ax, left=0, right=10) +ax.set_xlabel("Time") +ax.set_ylabel("Amplitude") +ax.set_title("OCEAN palette + GraphPadPrism") +legend(fig, ax, 3.1, 1.9) +save(fig, "graphpadprism_ocean") # --------------------------------------------------------------------------- # 4. Grouped bar chart with GraphPadPrism + AUTUMN palette # --------------------------------------------------------------------------- -with plt.style.context(STYLE): - fig, ax = plt.subplots(figsize=(7, 4)) - autumn = palettes.AUTUMN - groups = ["Q1", "Q2", "Q3", "Q4"] - x_pos = np.arange(len(groups)) - width = 0.25 - for i, label in enumerate(["Revenue", "Costs", "Profit"]): - rng = np.random.default_rng(i + 10) - vals = rng.integers(20, 80, len(groups)) - ax.bar(x_pos + i * width, vals, width, color=autumn[i * 3], label=label) - auto_ticks(ax, bottom=0, top=100) - # Set the group labels after auto_ticks, which replaces the major tick locators. - ax.set_xticks(x_pos + width, groups) - ax.xaxis.set_minor_locator(ticker.NullLocator()) - ax.set_xlim(x_pos[0] + width - 0.5, x_pos[-1] + width + 0.5) - ax.set_ylabel("Value") - ax.set_title("AUTUMN palette — grouped bar chart") - ax.legend() - fig.savefig(f"{OUT}/graphpadprism_grouped_bar.png", dpi=150, bbox_inches="tight") - plt.close(fig) +fig = plt.figure(figsize=(4, 2.25)) +ax = add_axes(fig, 0.5, 0.4, 2.5, 1.5) +autumn = palettes.AUTUMN +groups = ["Q1", "Q2", "Q3", "Q4"] +x_pos = np.arange(len(groups)) +width = 0.25 +for i, label in enumerate(["Revenue", "Costs", "Profit"]): + rng = np.random.default_rng(i + 10) + vals = rng.integers(20, 80, len(groups)) + ax.bar(x_pos + i * width, vals, width, color=autumn[i * 3], label=label) +auto_ticks(ax, bottom=0, top=100) +# Set the group labels after auto_ticks, which replaces the major tick locators. +ax.set_xticks(x_pos + width, groups) +ax.xaxis.set_minor_locator(ticker.NullLocator()) +ax.set_xlim(x_pos[0] + width - 0.5, x_pos[-1] + width + 0.5) +ax.set_ylabel("Value") +ax.set_title("AUTUMN palette — grouped bar chart") +legend(fig, ax, 3.1, 1.9) +save(fig, "graphpadprism_grouped_bar") # --------------------------------------------------------------------------- # 5. Scatter with error bars + GraphPadPrism + CORAL palette # --------------------------------------------------------------------------- -with plt.style.context(STYLE): - fig, ax = plt.subplots() - coral = palettes.CORAL - rng = np.random.default_rng(7) - for i in range(5): - xc = rng.normal(2 + i * 1.5, 0.4, 12) - yc = rng.normal(10 + i * 5, 2, 12) - ax.errorbar( - xc.mean(), - yc.mean(), - xerr=xc.std(), - yerr=yc.std(), - fmt="o", - color=coral[i * 2], - markersize=8, - capsize=4, - label=f"Group {i + 1}", - ) - auto_ticks(ax) - ax.set_xlabel("X measure") - ax.set_ylabel("Y measure") - ax.set_title("CORAL palette — error bars") - ax.legend(fontsize=7, loc="lower right") - fig.savefig(f"{OUT}/graphpadprism_errorbar.png", dpi=150, bbox_inches="tight") - plt.close(fig) +fig = plt.figure(figsize=(4, 2.25)) +ax = add_axes(fig, 0.5, 0.4, 2.5, 1.5) +coral = palettes.CORAL +rng = np.random.default_rng(7) +for i in range(5): + xc = rng.normal(2 + i * 1.5, 0.4, 12) + yc = rng.normal(10 + i * 5, 2, 12) + ax.errorbar( + xc.mean(), + yc.mean(), + xerr=xc.std(), + yerr=yc.std(), + fmt="o", + color=coral[i * 2], + markersize=8, + capsize=4, + label=f"Group {i + 1}", + ) +auto_ticks(ax) +ax.set_xlabel("X measure") +ax.set_ylabel("Y measure") +ax.set_title("CORAL palette — error bars") +legend(fig, ax, 3.1, 1.9) +save(fig, "graphpadprism_errorbar") print("All style examples saved to examples/")