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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ to [Semantic Versioning](https://semver.org/).
`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.
- `cylinder_icon` (a data store) and `chip_icon` (a processor) join `folder_icon` in `nicefigs.schematic`.
- `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.
Expand Down
4 changes: 4 additions & 0 deletions src/nicefigs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
from .ranking import caterpillar
from .scatter import scatter
from .schematic import (
chip_icon,
cylinder_icon,
folder_icon,
grid_cells,
group_band,
Expand Down Expand Up @@ -268,6 +270,8 @@
"group_band",
"label_pair",
"grid_cells",
"chip_icon",
"cylinder_icon",
"folder_icon",
"tint",
"shade",
Expand Down
113 changes: 112 additions & 1 deletion src/nicefigs/schematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
7 pt label is 7 pt on the page. :func:`schematic_axes` makes the canvas, with the x and y data
coordinates in inches; :func:`~nicefigs.diagram_box` and :func:`~nicefigs.diagram_arrow` draw
boxes and connectors on it, and this module adds the remaining pieces: :func:`numbered_disc`,
:func:`group_band`, :func:`label_pair`, :func:`grid_cells`, :func:`folder_icon`, and the colour
:func:`group_band`, :func:`label_pair`, :func:`grid_cells`, :func:`folder_icon`,
:func:`cylinder_icon`, :func:`chip_icon`, and the colour
helpers :func:`tint` and :func:`shade`. Every colour defaults to the active theme and the
:data:`~nicefigs.NICE_COLORS`: ink is the theme's text colour, secondary text the neutral grey,
the accent the orange of the house palette.
Expand Down Expand Up @@ -40,6 +41,8 @@
from matplotlib.text import Text

__all__ = [
"chip_icon",
"cylinder_icon",
"folder_icon",
"grid_cells",
"group_band",
Expand Down Expand Up @@ -358,3 +361,111 @@ def folder_icon(
)
ax.add_patch(icon)
return icon


def cylinder_icon(
ax: Axes,
xy: tuple[float, float],
width: float,
height: float,
*,
facecolor: Any = None,
edgecolor: Any = None,
linewidth: float = 0.6,
zorder: float = 3.0,
) -> tuple[Polygon, Polygon]:
"""Draw a cylinder icon, to stand for a data store or database.

Args:
ax: Axes to draw on.
xy: Lower-left corner of the icon's bounding box, in data coordinates.
width: Icon width, in data units.
height: Icon height, in data units, rim included.
facecolor: Fill colour; a light tint of the theme's text colour by default.
edgecolor: Outline colour; the theme's text colour by default.
linewidth: Outline width (pt).
zorder: Drawing order; the top rim draws just above the body.

Returns:
The body and the top rim patches.
"""
th = get_theme()
face = tint(th.text_color, 0.12) if facecolor is None else facecolor
edge = th.text_color if edgecolor is None else edgecolor
x, y = xy
a, b = width / 2, min(0.14 * width, 0.2 * height)
cx = x + a
t = np.linspace(0.0, np.pi, 41)
lower = np.column_stack([cx - a * np.cos(t), y + b - b * np.sin(t)])
upper = np.column_stack([cx + a * np.cos(t), y + height - b + b * np.sin(t)])
body = Polygon(
np.vstack([lower, upper]),
closed=True,
facecolor=face,
edgecolor=edge,
linewidth=linewidth,
zorder=zorder,
)
s = np.linspace(0.0, 2 * np.pi, 81)
rim = Polygon(
np.column_stack([cx + a * np.cos(s), y + height - b + b * np.sin(s)]),
closed=True,
facecolor=face,
edgecolor=edge,
linewidth=linewidth,
zorder=zorder + 0.01,
)
ax.add_patch(body)
ax.add_patch(rim)
return body, rim


def chip_icon(
ax: Axes,
center: tuple[float, float],
size: float,
*,
pins: int = 3,
facecolor: Any = None,
edgecolor: Any = None,
linewidth: float = 0.6,
zorder: float = 3.0,
) -> Polygon:
"""Draw a processor chip icon, a square die with pins on every side, to stand for a CPU or GPU.

Args:
ax: Axes to draw on.
center: Centre, in data coordinates.
size: Side of the icon with its pins, in data units; the die is 70 % of it.
pins: Pins per side.
facecolor: Fill colour of the die; a light tint of the theme's text colour by default.
edgecolor: Outline and pin colour; the theme's text colour by default.
linewidth: Outline and pin width (pt).
zorder: Drawing order.

Returns:
The die patch.
"""
th = get_theme()
edge = th.text_color if edgecolor is None else edgecolor
cx, cy = center
h, d = size / 2, 0.35 * size
die = Polygon(
[(cx - d, cy - d), (cx + d, cy - d), (cx + d, cy + d), (cx - d, cy + d)],
closed=True,
facecolor=tint(th.text_color, 0.12) if facecolor is None else facecolor,
edgecolor=edge,
linewidth=linewidth,
zorder=zorder,
)
ax.add_patch(die)
for k in range(pins):
u = -d + (k + 1) * 2 * d / (pins + 1)
for xs, ys in (
((cx + u, cx + u), (cy + d, cy + h)),
((cx + u, cx + u), (cy - d, cy - h)),
((cx - d, cx - h), (cy + u, cy + u)),
((cx + d, cx + h), (cy + u, cy + u)),
):
ax.plot(xs, ys, color=edge, linewidth=linewidth, solid_capstyle="butt", zorder=zorder - 0.01)
return die
21 changes: 21 additions & 0 deletions tests/test_schematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ def test_folder_icon_stays_inside_its_box():
assert pts[:, 1].max() == pytest.approx(0.6)


def test_cylinder_icon_stays_inside_its_box_and_shows_its_rim():
fig, ax = nf.schematic_axes(1.0, 1.0)
body, rim = nf.cylinder_icon(ax, (0.2, 0.3), 0.4, 0.5)
pts = np.vstack([np.asarray(body.get_xy()), np.asarray(rim.get_xy())])
assert pts[:, 0].min() == pytest.approx(0.2, abs=1e-9)
assert pts[:, 0].max() == pytest.approx(0.6, abs=1e-9)
assert pts[:, 1].min() == pytest.approx(0.3, abs=1e-9)
assert pts[:, 1].max() == pytest.approx(0.8, abs=1e-9)
assert rim.zorder > body.zorder


def test_chip_icon_is_a_square_with_pins_on_every_side():
fig, ax = nf.schematic_axes(1.0, 1.0)
die = nf.chip_icon(ax, (0.5, 0.5), 0.4, pins=3)
pts = np.asarray(die.get_xy())
assert pts[:, 0].max() - pts[:, 0].min() == pytest.approx(0.4 * 0.7)
assert len(ax.lines) == 12
xs = np.concatenate([line.get_xdata() for line in ax.lines])
assert xs.min() == pytest.approx(0.3) and xs.max() == pytest.approx(0.7)


def test_save_with_check_text_refuses_overlapping_text(tmp_path):
fig, ax = nf.schematic_axes(2.0, 1.0)
ax.text(1.0, 0.5, "first label")
Expand Down
Loading