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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ to [Semantic Versioning](https://semver.org/).

### Added

- Concept-figure primitives in `nicefigs.schematic`: `schematic_axes` (a figure of the printed size
whose data units are inches), `numbered_disc`, `group_band`, `label_pair`, `grid_cells`,
`folder_icon` and the colour helpers `tint`, `shade` and `soft_color`. They draw with the active
theme and the `NICE_COLORS` of the house palette, and combine with `diagram_box` and
`diagram_arrow`. `NICE_COLORS` is now exported at the top level.
- `save(..., check_text=True)` runs `check_text_overlaps` first and raises `ValueError` on a problem.
- Gallery example `35_schematic`.
- The font chain reaches metric-compatible Helvetica clones (TeX Gyre Heros, Nimbus Sans, Liberation Sans) before DejaVu Sans.
- `register_fonts(dir)` (default `$NICEFIGS_FONT_DIR`) makes a folder of fonts available to a theme; the themes' fonts
are unchanged. SVG text is written as paths (`svg.fonttype="path"` in every theme and in `save`); text
Expand Down
4 changes: 4 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ and the [Gallery](gallery.md) for runnable examples.

::: nicefigs.diagrams

## `nicefigs.schematic`

::: nicefigs.schematic

## `nicefigs.sections`

::: nicefigs.sections
Expand Down
16 changes: 16 additions & 0 deletions docs/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,19 @@ spot.
```python
--8<-- "examples/31_panel_label_with_title.py"
```

### 35 — Concept figure from schematic primitives {#schematic}

[`schematic_axes`][nicefigs.schematic_axes] gives a figure of the printed size whose data units
are inches, on which [`diagram_box`][nicefigs.diagram_box] and
[`diagram_arrow`][nicefigs.diagram_arrow] draw the flow, and
[`numbered_disc`][nicefigs.numbered_disc], [`group_band`][nicefigs.group_band],
[`label_pair`][nicefigs.label_pair], [`grid_cells`][nicefigs.grid_cells] and
[`folder_icon`][nicefigs.folder_icon] add the numbering, grouping, labels and glyphs. Colours come
from the active theme and the `NICE_COLORS`.

![Schematic](gallery/35_schematic.png)

```python
--8<-- "examples/35_schematic.py"
```
Binary file added docs/gallery/35_schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/35_schematic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Schematic: a small pipeline drawn at its printed size from boxes, arrows, discs, bands and glyphs."""

import nicefigs as nf


def make():
nf.set_theme(nf.Theme.from_base(7.2))
fig, ax = nf.schematic_axes(5.5, 1.9)
th = nf.get_theme()
blue, orange = nf.NICE_COLORS["blue"], nf.NICE_COLORS["orange"]
nf.group_band(ax, (1.7, 0.1), 2.4, 1.7, "run chunk by chunk")
steps = [
(0.8, "read", "target and prediction"),
(2.9, "compare", "view, then metric"),
(4.7, "store", "one folder each"),
]
for k, (x, name, detail) in enumerate(steps):
fill = nf.tint(blue, 0.2) if k == 1 else "white"
nf.diagram_box(
ax, (x, 1.2), 1.3, 0.5, facecolor=fill, edgecolor=th.text_color, linewidth=0.7, rounding=0.03
)
nf.label_pair(ax, (x, 1.2), name, detail, bold=True)
nf.numbered_disc(ax, (x - 0.65, 1.45), k + 1)
for x0, x1 in ((1.45, 2.25), (3.55, 4.05)):
nf.diagram_arrow(ax, (x0, 1.2), (x1, 1.2), style="line", width=0.7, head_length=3.4, head_width=3.0)
nf.grid_cells(ax, (0.3, 0.2), 5, 3, 0.15)
nf.grid_cells(ax, (2.15, 0.2), 5, 3, 0.15, fills={(2, 1): nf.tint(orange, 0.5)})
for k in range(3):
nf.folder_icon(ax, (4.2 + k * 0.3, 0.3), 0.24, 0.18)
return fig


if __name__ == "__main__":
import matplotlib

matplotlib.use("Agg")
nf.save(make(), "35_schematic", formats=("png", "pdf"), check_text=True)
24 changes: 23 additions & 1 deletion src/nicefigs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from . import data
from .animation import animate_maps, render_animation, save_animation, stitch_frames
from .artists import ARTIST_PALETTES, HOCKNEY_CANYON, NICE, artist_cmap, artist_palette
from .artists import ARTIST_PALETTES, HOCKNEY_CANYON, NICE, NICE_COLORS, artist_cmap, artist_palette
from .bars import grouped as grouped_bars
from .bars import histogram
from .bars import stacked as stacked_bars
Expand Down Expand Up @@ -94,6 +94,17 @@
from .pareto import frontier, pareto
from .ranking import caterpillar
from .scatter import scatter
from .schematic import (
folder_icon,
grid_cells,
group_band,
label_pair,
numbered_disc,
schematic_axes,
shade,
soft_color,
tint,
)
from .sections import cross_section, cross_section_panels, profile
from .shapes import polygon_mesh
from .spectra import spectrum
Expand Down Expand Up @@ -200,6 +211,7 @@
"ARTIST_PALETTES",
"HOCKNEY_CANYON",
"NICE",
"NICE_COLORS",
# colorbars
"colorbar",
"inset_histogram_colorbar",
Expand Down Expand Up @@ -249,6 +261,16 @@
"scorecard",
"skill_gap",
"diagram_box",
# schematics
"schematic_axes",
"numbered_disc",
"group_band",
"label_pair",
"grid_cells",
"folder_icon",
"tint",
"shade",
"soft_color",
"diagram_arrow",
"flux_diagram",
"flux_legend",
Expand Down
7 changes: 7 additions & 0 deletions src/nicefigs/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def save(
dpi: int | None = None,
transparent: bool = False,
close: bool = False,
check_text: bool = False,
) -> list[Path]:
"""Save ``fig`` with a tight bounding box, creating parent directories.

Expand All @@ -209,14 +210,20 @@ def save(
dpi: Raster resolution; defaults to the active theme's ``savedpi``.
transparent: Transparent background.
close: Close the figure after saving.
check_text: Run :func:`check_text_overlaps` first and raise if it finds a problem.

Returns:
The written paths, one per format, in the order of ``formats``.

Raises:
ValueError: ``check_text`` is set and some text overlaps another or leaves the figure.

Example::

nf.save(fig, "figures/fig01", formats=("pdf", "png"))
"""
if check_text and (issues := check_text_overlaps(fig)):
raise ValueError(f"text layout problems: {issues}")
p = Path(path)
th = get_theme()
dpi = dpi or th.savedpi
Expand Down
Loading
Loading