Skip to content

carto_flow.symbol_cartogram.tiling

Tiling abstractions for tile-based symbol cartograms.

This module defines the Tiling ABC and concrete tiling classes that generate congruent tilings of the plane. Each tiling produces a TilingResult containing tile polygons, per-tile transforms, and a precomputed adjacency matrix.

Tiling Classes

SquareTiling Regular square grid. HexagonTiling Regular hexagonal grid (pointy-top). TriangleTiling Tiling by congruent triangles (any triangle tiles the plane). QuadrilateralTiling Tiling by congruent quadrilaterals (any simple quadrilateral tiles the plane).

Classes:

Functions:

  • resolve_tiling

    Resolve a tiling specification to a Tiling instance.

HexagonTiling

HexagonTiling(pointy_top: bool = True)

Bases: Tiling

Regular hexagonal grid tiling (pointy-top orientation).

Parameters:

  • pointy_top (bool, default: True ) –

    If True (default), hexagons have a vertex at top.

Methods:

Attributes:

canonical_tile property

canonical_tile: Polygon

Unit hexagon centered at origin.

canonical_symbol

canonical_symbol() -> Symbol

Return a HexagonSymbol matching this tiling's orientation.

from_polygon classmethod

from_polygon(polygon: Polygon) -> HexagonTiling

Create from polygon (must be a hexagon; ignored for HexagonTiling).

tile_size_for_symbol_size

tile_size_for_symbol_size(
    symbol_size: float, spacing: float = 0.0
) -> float

Compute tile_size for hexagon tiling.

For HexagonTiling, tile_size is the circumradius (center to vertex). HexagonSymbol has bounding_radius = 0.5, so after scaling by 2*size, the circumradius equals symbol_size.

Therefore: tile_size = symbol_size * (1 + spacing)

IsohedralTiling

IsohedralTiling(
    tiling_type: int,
    parameters: list[float] | None = None,
    edge_curves: dict[int, list[tuple[float, float]]]
    | None = None,
    edge_curve_modes: dict[int, str] | None = None,
)

Bases: Tiling

Isohedral tiling based on the Grünbaum-Shephard classification.

Wraps the tactile library to generate any of 81 isohedral tiling types. Each type has 0-6 free parameters controlling tile vertex positions, and supports optional custom edge curves for Escher-style tiles.

Parameters:

  • tiling_type (int) –

    Isohedral type number (IH1-IH93, with some gaps). Use IsohedralTiling.available_types() for the full list.

  • parameters (list[float], default: None ) –

    Free parameters for the tiling type. If None, uses defaults.

  • edge_curves (dict[int, list[tuple[float, float]]], default: None ) –

    Custom edge curves keyed by edge shape ID. Each curve is a list of (x, y) points in canonical space from (0, 0) to (1, 0). Symmetry constraints (S, U) are enforced automatically. Edge shape IDs and types can be inspected via type_info(). If None, all edges are straight lines.

Methods:

  • available_types

    Return list of valid isohedral tiling type numbers.

  • canonical_symbol

    Return an IsohedralTileSymbol with this tiling's parameters and curves.

  • describe

    Return a human-readable summary of an isohedral tiling type.

  • find_types

    Find isohedral tiling types matching the given criteria.

  • from_dict

    Load a tiling from a serialized dictionary.

  • from_polygon

    Not supported — use IsohedralTiling(tiling_type, parameters).

  • from_preset

    Create an IsohedralTiling from a named preset.

  • list_presets

    Return a dict mapping preset names to their descriptions.

  • load

    Load a tiling configuration from a JSON file.

  • plot_prototile

    Plot the prototile with labeled, color-coded edges.

  • save

    Save the tiling configuration to a JSON file.

  • tile_size_for_symbol_size

    Compute tile_size for isohedral tiling.

  • to_dict

    Serialize the tiling configuration to a dictionary.

  • type_info

    Return metadata for an isohedral tiling type.

Attributes:

  • canonical_tile (Polygon) –

    Unit-area prototile centered at origin.

canonical_tile property

canonical_tile: Polygon

Unit-area prototile centered at origin.

available_types staticmethod

available_types() -> list[int]

Return list of valid isohedral tiling type numbers.

canonical_symbol

canonical_symbol() -> Symbol

Return an IsohedralTileSymbol with this tiling's parameters and curves.

describe

describe(
    self_or_type: IsohedralTiling | None = None,
    tiling_type: int | None = None,
) -> str

Return a human-readable summary of an isohedral tiling type.

Can be called on an instance (t.describe()) or on the class (IsohedralTiling.describe(83)).

Parameters:

  • tiling_type (int, default: None ) –

    Isohedral type number. Not needed when called on an instance.

Returns:

  • str

    Multi-line description of the tiling type.

find_types staticmethod

find_types(
    *,
    num_vertices: int | None = None,
    num_parameters: int | None = None,
    max_parameters: int | None = None,
    num_aspects: int | None = None,
    has_reflections: bool | None = None,
    edge_types: set[str] | None = None,
    all_edges: str | None = None,
    has_customizable_edges: bool | None = None,
) -> list[int]

Find isohedral tiling types matching the given criteria.

All arguments are optional filters. Only types satisfying every specified filter are returned.

Parameters:

  • num_vertices (int, default: None ) –

    Exact number of prototile vertices (3, 4, 5, or 6).

  • num_parameters (int, default: None ) –

    Exact number of free parameters.

  • max_parameters (int, default: None ) –

    Maximum number of free parameters.

  • num_aspects (int, default: None ) –

    Exact number of aspects (copies per fundamental domain).

  • has_reflections (bool, default: None ) –

    If True, only types with reflected aspects. If False, only types without reflections.

  • edge_types (set of str, default: None ) –

    Required edge types, e.g. {"J", "S"}. Only types whose edges include all listed types are returned.

  • all_edges (str, default: None ) –

    If set, only types where every edge is this type (e.g. "J").

  • has_customizable_edges (bool, default: None ) –

    If True, at least one edge is J, S, or U. If False, all edges are I (straight).

Returns:

  • list[int]

    Matching isohedral type numbers, sorted.

Examples:

>>> IsohedralTiling.find_types(num_vertices=3, all_edges="J")
[]  # no triangular type has all-J edges
>>> IsohedralTiling.find_types(num_vertices=4, all_edges="J")
[41, 43, 44, 52, 55, 59, 61, 68, 71]
>>> IsohedralTiling.find_types(num_vertices=6, max_parameters=0)
[10, 31, 33, 34, 36]

from_dict classmethod

from_dict(data: dict) -> IsohedralTiling

Load a tiling from a serialized dictionary.

Parameters:

  • data (dict) –

    Dictionary as produced by :meth:to_dict.

Returns:

from_polygon classmethod

from_polygon(polygon: Polygon) -> IsohedralTiling

Not supported — use IsohedralTiling(tiling_type, parameters).

from_preset classmethod

from_preset(name: str, **kwargs) -> IsohedralTiling

Create an IsohedralTiling from a named preset.

Parameters:

  • name (str) –

    Preset name. Use IsohedralTiling.list_presets() for options.

  • **kwargs

    Overrides passed to IsohedralTiling(), e.g. edge_curves, parameters.

Returns:

list_presets classmethod

list_presets() -> dict[str, str]

Return a dict mapping preset names to their descriptions.

Returns:

  • dict[str, str]

load classmethod

load(path: str | Path) -> IsohedralTiling

Load a tiling configuration from a JSON file.

Parameters:

  • path (str or Path) –

    Path to a JSON file previously written by :meth:save.

Returns:

plot_prototile

plot_prototile(ax=None, show_labels: bool = True)

Plot the prototile with labeled, color-coded edges.

Parameters:

  • ax (Axes, default: None ) –

    Axes to draw on. If None, a new figure is created.

  • show_labels (bool, default: True ) –

    Whether to annotate edges with their ID and type.

Returns:

  • Axes

save

save(path: str | Path) -> None

Save the tiling configuration to a JSON file.

Parameters:

  • path (str or Path) –

    Destination file path. The file is created or overwritten.

tile_size_for_symbol_size

tile_size_for_symbol_size(
    symbol_size: float, spacing: float = 0.0
) -> float

Compute tile_size for isohedral tiling.

IsohedralTiling uses unit-area tiles. TileSymbol normalizes to bounding_radius = 0.5, so we need to compute the original tile's bounding radius to find the correct scale factor.

to_dict

to_dict() -> dict

Serialize the tiling configuration to a dictionary.

Returns:

  • dict

    Dictionary with keys tiling_type, parameters, and edge_curves suitable for passing to :meth:from_dict or saving as JSON.

type_info

type_info(
    self_or_type: IsohedralTiling | None = None,
    tiling_type: int | None = None,
) -> dict

Return metadata for an isohedral tiling type.

Can be called on an instance (t.type_info()) or on the class (IsohedralTiling.type_info(83)).

Parameters:

  • tiling_type (int, default: None ) –

    Isohedral type number. Not needed when called on an instance.

Returns:

  • dict

    Keys include num_parameters, num_vertices, num_aspects, num_edge_shapes, edge_shapes, default_parameters, edges, has_reflections, customizable_shape_ids, parameters.

    The edges list has one entry per prototile edge with keys: edge_index, shape_id, shape_type, description, customizable.

QuadrilateralTiling

QuadrilateralTiling(tile: Polygon)

Bases: Tiling

Tiling by congruent quadrilaterals.

Any simple quadrilateral tiles the Euclidean plane via 180° rotation about edge midpoints.

Parameters:

  • tile (Polygon) –

    A quadrilateral (4-vertex polygon). Will be normalized to unit area internally. The shape (angles/proportions) is preserved.

Methods:

Attributes:

  • canonical_tile (Polygon) –

    Unit-area quadrilateral centered at origin.

canonical_tile property

canonical_tile: Polygon

Unit-area quadrilateral centered at origin.

canonical_symbol

canonical_symbol() -> Symbol

Return a TileSymbol wrapping this quadrilateral's canonical tile.

from_polygon classmethod

from_polygon(polygon: Polygon) -> QuadrilateralTiling

Create a tiling from a user-supplied quadrilateral.

parallelogram classmethod

parallelogram(
    angle: float = 60.0, aspect_ratio: float = 1.0
) -> QuadrilateralTiling

Parallelogram with given angle and aspect ratio.

Parameters:

  • angle (float, default: 60.0 ) –

    Interior angle in degrees (0 < angle < 180).

  • aspect_ratio (float, default: 1.0 ) –

    Ratio of side b to side a.

rectangle classmethod

rectangle(aspect_ratio: float = 1.0) -> QuadrilateralTiling

Rectangle with given aspect ratio (height/width).

Parameters:

  • aspect_ratio (float, default: 1.0 ) –

    Ratio of height to width. Default 1.0 gives a square.

rhombus classmethod

rhombus(angle: float = 60.0) -> QuadrilateralTiling

Rhombus (all sides equal) with given angle.

Parameters:

  • angle (float, default: 60.0 ) –

    Interior angle in degrees.

tile_size_for_symbol_size

tile_size_for_symbol_size(
    symbol_size: float, spacing: float = 0.0
) -> float

Compute tile_size for quadrilateral tiling.

QuadrilateralTiling uses unit-area tiles. TileSymbol normalizes to bounding_radius = 0.5, so we need to compute the original tile's bounding radius to find the correct scale factor.

trapezoid classmethod

trapezoid(
    top_ratio: float = 0.5, height: float = 1.0
) -> QuadrilateralTiling

Isosceles trapezoid.

Parameters:

  • top_ratio (float, default: 0.5 ) –

    Ratio of top edge to bottom edge. Must be < 1.

  • height (float, default: 1.0 ) –

    Height of the trapezoid.

SquareTiling

Bases: Tiling

Regular square grid tiling.

All tiles are axis-aligned squares of equal size.

Methods:

  • canonical_symbol

    Return a SquareSymbol for this tiling.

  • from_polygon

    Create from polygon (must be a square; ignored for SquareTiling).

Attributes:

canonical_tile property

canonical_tile: Polygon

Unit square centered at origin.

canonical_symbol

canonical_symbol() -> Symbol

Return a SquareSymbol for this tiling.

from_polygon classmethod

from_polygon(polygon: Polygon) -> SquareTiling

Create from polygon (must be a square; ignored for SquareTiling).

TileAdjacencyType

Bases: str, Enum

How tile adjacency is determined.

TileTransform dataclass

TileTransform(
    center: tuple[float, float],
    rotation: float = 0.0,
    flipped: bool = False,
)

Transform for a single tile: center position, rotation, and flip.

Attributes:

  • center (tuple[float, float]) –

    Tile center in world coordinates.

  • rotation (float) –

    Rotation angle in degrees (counter-clockwise).

  • flipped (bool) –

    Whether the tile is reflected about the vertical axis (before rotation).

Tiling

Bases: ABC

A congruent tiling of the plane.

Subclasses implement specific tiling patterns. The generate method produces tiles covering a bounding box.

Methods:

  • canonical_symbol

    Return the Symbol matching this tiling's tile shape.

  • from_polygon

    Create a tiling from a user-supplied polygon.

  • generate

    Generate tiles covering the bounding box.

  • plot_tile

    Plot the canonical (proto)tile shape using matplotlib.

  • tile_size_for_symbol_size

    Compute tile_size so that a symbol of given size fills the tile.

Attributes:

  • canonical_tile (Polygon) –

    The tile shape at unit size, centered at origin.

canonical_tile abstractmethod property

canonical_tile: Polygon

The tile shape at unit size, centered at origin.

canonical_symbol abstractmethod

canonical_symbol() -> Symbol

Return the Symbol matching this tiling's tile shape.

Returns:

  • Symbol

    A symbol instance that can render tiles from this tiling.

from_polygon abstractmethod classmethod

from_polygon(polygon: Polygon) -> Self

Create a tiling from a user-supplied polygon.

generate abstractmethod

generate(
    bounds: tuple[
        float, float, float, float
    ] = DEFAULT_BOUNDS,
    n_tiles: int | None = None,
    tile_size: float | None = None,
    margin: float = 0.1,
    adjacency_type: TileAdjacencyType = TileAdjacencyType.EDGE,
) -> TilingResult

Generate tiles covering the bounding box.

Parameters:

  • bounds (tuple, default: DEFAULT_BOUNDS ) –

    Bounding box as (minx, miny, maxx, maxy). Defaults to the unit box (-1, -1, 1, 1) (see :data:DEFAULT_BOUNDS).

  • n_tiles (int, default: None ) –

    Approximate number of tiles. Mutually exclusive with tile_size. When neither is given, defaults to :data:DEFAULT_N_TILES.

  • tile_size (float, default: None ) –

    Size of each tile. Mutually exclusive with n_tiles.

  • margin (float, default: 0.1 ) –

    Fractional margin around bounding box.

  • adjacency_type (TileAdjacencyType, default: EDGE ) –

    Whether adjacency requires shared edges or just shared vertices.

Returns:

plot_tile

plot_tile(
    ax=None,
    *,
    face_color: str | None = None,
    edge_color: str = "#1971c2",
    alpha: float = 0.7,
    show_vertices: bool = True,
    show_vertex_labels: bool = False,
    linewidth: float = 1.5,
)

Plot the canonical (proto)tile shape using matplotlib.

No call to :meth:generate is needed — the tile is derived directly from the tiling's :attr:canonical_tile property.

Parameters:

  • ax (Axes, default: None ) –

    Axes to draw on. A new figure is created if ax is None.

  • face_color (str, default: None ) –

    Fill colour. When None the colour is chosen automatically by vertex count to match the panel UI palette (3 → pink, 4 → blue, 5 → yellow, 6 → green).

  • edge_color (str, default: '#1971c2' ) –

    Outline colour.

  • alpha (float, default: 0.7 ) –

    Fill transparency.

  • show_vertices (bool, default: True ) –

    Draw a dot at each tile vertex.

  • show_vertex_labels (bool, default: False ) –

    Annotate each vertex with its 0-based index.

  • linewidth (float, default: 1.5 ) –

    Outline width in points.

Returns:

  • PrototilePlotResult

    Named result with ax, fig, tile (the patch), and vertices (scatter artist, or None).

tile_size_for_symbol_size

tile_size_for_symbol_size(
    symbol_size: float, spacing: float = 0.0
) -> float

Compute tile_size so that a symbol of given size fills the tile.

The symbol is defined in the unit square [-0.5, 0.5]² and scaled by 2 * symbol_size. This method computes the appropriate tile_size parameter for the tiling's generate() method so that the tile matches the scaled symbol.

Parameters:

  • symbol_size (float) –

    The native half-extent of the symbol (after area_factor conversion).

  • spacing (float, default: 0.0 ) –

    Additional spacing as a fraction of symbol size (default 0.0).

Returns:

  • float

    The tile_size parameter to pass to generate().

Notes

Different tilings interpret tile_size differently: - SquareTiling: tile_size = side length - HexagonTiling: tile_size = circumradius (center to vertex) - TriangleTiling: tile_size = scale factor for unit-area tile

TilingResult dataclass

TilingResult(
    polygons: list[Polygon],
    transforms: list[TileTransform],
    adjacency: NDArray[bool_],
    vertex_adjacency: NDArray[bool_] | None,
    tile_size: float,
    inscribed_radius: float,
    canonical_tile: Polygon,
    n_base_vertices: int | None = None,
)

Output of tiling generation.

Attributes:

  • polygons (list[Polygon]) –

    Tile polygons in world coordinates.

  • transforms (list[TileTransform]) –

    Per-tile transforms (center, rotation, flip).

  • adjacency (NDArray[bool_]) –

    (m, m) boolean edge adjacency matrix between tiles.

  • vertex_adjacency (NDArray[bool_] | None) –

    (m, m) boolean vertex-only adjacency matrix (tiles sharing exactly one vertex but no edge). None for tilings that don't compute it.

  • tile_size (float) –

    Size parameter used for generation.

  • inscribed_radius (float) –

    Radius of the largest circle fitting inside the canonical tile at the generated size.

  • canonical_tile (Polygon) –

    The unit tile shape (before transforms), at the generated size.

  • n_base_vertices (int or None) –

    Topological vertex count of the prototile. For most tilings this equals len(canonical_tile.exterior.coords) - 1, but for isohedral tilings with custom edge curves the canonical_tile polygon has many more points due to spline interpolation. Used by :meth:plot_tile to select the correct auto-colour.

Methods:

  • plot

    Plot the tiling using matplotlib.

  • plot_tile

    Plot the canonical (proto)tile shape using matplotlib.

  • rotate

    Rotate all tile positions and orientations.

centers property

centers: NDArray[floating]

(m, 2) array of tile centers.

n_tiles property

n_tiles: int

Number of tiles.

plot

plot(
    ax=None,
    *,
    color_by: str = "uniform",
    face_color: str = "#4e9af1",
    edge_color: str = "#333333",
    colormap: str = "tab10",
    alpha: float = 0.85,
    linewidth: float = 0.5,
)

Plot the tiling using matplotlib.

Parameters:

  • ax (Axes, default: None ) –

    Axes to draw on. A new figure is created if ax is None.

  • color_by (('uniform', 'aspect', 'index', 'x', 'y', 'random'), default: "uniform" ) –

    Colouring scheme:

    • "uniform" — all tiles use face_color.
    • "aspect" — colour by tile rotation angle (0–360 → colormap).
    • "index" — colour by tile index (0…n−1 → colormap).
    • "x" — colour by tile centroid x coordinate (normalised).
    • "y" — colour by tile centroid y coordinate (normalised).
    • "random" — random colour per tile drawn from colormap.
  • face_color (str, default: '#4e9af1' ) –

    Hex fill colour used in "uniform" mode.

  • edge_color (str, default: '#333333' ) –

    Stroke colour for tile outlines.

  • colormap (str, default: 'tab10' ) –

    Matplotlib colormap name used in "aspect" and "index" modes.

  • alpha (float, default: 0.85 ) –

    Tile fill transparency.

  • linewidth (float, default: 0.5 ) –

    Tile outline width in points.

Returns:

  • TilingGridPlotResult

    Named result with ax, fig, and tiles (the PolyCollection).

Examples:

>>> from carto_flow.symbol_cartogram import SquareTiling
>>> pr = SquareTiling().generate().plot(color_by="index")
>>> pr.tiles.set_alpha(0.5)

plot_tile

plot_tile(
    ax=None,
    *,
    face_color: str | None = None,
    edge_color: str = "#1971c2",
    alpha: float = 0.7,
    show_vertices: bool = True,
    show_vertex_labels: bool = False,
    linewidth: float = 1.5,
)

Plot the canonical (proto)tile shape using matplotlib.

Parameters:

  • ax (Axes, default: None ) –

    Axes to draw on. A new figure is created if ax is None.

  • face_color (str, default: None ) –

    Fill colour. When None the colour is chosen automatically by vertex count to match the panel UI palette (3 → pink, 4 → blue, 5 → yellow, 6 → green).

  • edge_color (str, default: '#1971c2' ) –

    Outline colour.

  • alpha (float, default: 0.7 ) –

    Fill transparency.

  • show_vertices (bool, default: True ) –

    Draw a dot at each tile vertex.

  • show_vertex_labels (bool, default: False ) –

    Annotate each vertex with its 0-based index.

  • linewidth (float, default: 1.5 ) –

    Outline width in points.

Returns:

  • PrototilePlotResult

    Named result with ax, fig, tile (the patch), and vertices (scatter artist, or None).

Examples:

>>> from carto_flow.symbol_cartogram import IsohedralTiling
>>> result = IsohedralTiling(1).generate()
>>> pr = result.plot_tile(show_vertex_labels=True)
>>> pr.tile.set_facecolor("coral")

rotate

rotate(angle: float) -> TilingResult

Rotate all tile positions and orientations.

Returns a new TilingResult with all tile centers rotated about the origin and all tile rotations incremented by the given angle.

Parameters:

  • angle (float) –

    Rotation angle in degrees (counter-clockwise).

Returns:

Notes

The adjacency matrices are preserved since rotation is a rigid transformation that doesn't change neighbor relationships.

TriangleTiling

TriangleTiling(tile: Polygon)

Bases: Tiling

Tiling by congruent triangles.

Any triangle tiles the Euclidean plane. The tiling is constructed by pairing triangles via 180° rotation about an edge midpoint to form parallelograms, which then tile by translation.

Parameters:

  • tile (Polygon) –

    A triangle (3-vertex polygon). Will be normalized to unit area internally. The shape (angles/proportions) is preserved.

Methods:

Attributes:

canonical_tile property

canonical_tile: Polygon

Unit-area triangle centered at origin.

canonical_symbol

canonical_symbol() -> Symbol

Return a TileSymbol wrapping this triangle's canonical tile.

equilateral classmethod

equilateral() -> TriangleTiling

Equilateral triangle tiling.

from_polygon classmethod

from_polygon(polygon: Polygon) -> TriangleTiling

Create a tiling from a user-supplied triangle.

isosceles classmethod

isosceles(apex_angle: float = 60.0) -> TriangleTiling

Isosceles triangle with given apex angle (degrees).

Parameters:

  • apex_angle (float, default: 60.0 ) –

    Angle at the apex in degrees. Default 60.0 gives equilateral.

right classmethod

right(aspect_ratio: float = 1.0) -> TriangleTiling

Right triangle with given aspect ratio (height/base).

Parameters:

  • aspect_ratio (float, default: 1.0 ) –

    Ratio of height to base. Default 1.0 gives a right isosceles.

right_isosceles classmethod

right_isosceles() -> TriangleTiling

Right isosceles triangle tiling (45-45-90).

tile_size_for_symbol_size

tile_size_for_symbol_size(
    symbol_size: float, spacing: float = 0.0
) -> float

Compute tile_size for triangle tiling.

TriangleTiling uses unit-area tiles. TileSymbol normalizes to bounding_radius = 0.5, so we need to compute the original tile's bounding radius to find the correct scale factor.

resolve_tiling

resolve_tiling(spec: str | Tiling) -> Tiling

Resolve a tiling specification to a Tiling instance.

Parameters:

  • spec (str or Tiling) –

    A string shorthand ("square", "hexagon", "triangle", "quadrilateral", "isohedral", "ih<N>"), or a Tiling instance.

Returns: