API reference

andrey.viz.draw#

andrey.viz.draw(structure, *, dark=False, palette=None, width=None, height=None, background=True, positions=None, engine='auto', engine_options=None, highlight=())[source]#

Render a causal structure to an SVG string.

Parameters:
  • structure (GraphStructure) – Any kind (dag / cpdag / pag / digraph). Its .node_types flag latent variables, which are drawn hollow and dashed. If every label has at most two characters, all labels sit inside nodes; otherwise, all labels sit below them.

  • dark (bool) – Select the built-in dark palette instead of the light one. Ignored when palette is set.

  • palette (Palette | None) – A Palette (andrey.viz.LIGHT / DARK or a custom one); overrides dark.

  • width (int | None) – Canvas width in pixels. None sizes the canvas to the layout, with every node and label clear of the others. Either way the layout keeps its proportions and is centered. The SVG is fluid (width="100%"); this sets the coordinate system. A width too narrow for the widest label and the margins is widened to the narrowest that holds them.

  • height (int | None) – Canvas height; None sizes it to the layout. A height too short for the margins is raised the same way.

  • background (bool) – Paint the canvas with the palette’s paper color. Set False for a transparent SVG to composite onto an already-themed page.

  • positions (Mapping[int, tuple[float, float]] | None) – Each node’s (x, y) in the unit square, y downward, as layout() returns. None uses layout() with engine and engine_options.

  • engine (str) – The layout layout() runs when positions is None: "dot" and Graphviz’s other layouts, or "builtin". "auto" is "dot" when pygraphviz is installed.

  • engine_options (Mapping[str, object] | None) – Graphviz graph attributes for the layout, such as {"rankdir": "TB"}.

  • highlight (Iterable[tuple[int, int]]) – Endpoints to light in the palette’s accent color. (i, j) lights the mark at node i on the edge between nodes i and j, and that half of the edge, with a soft glow under it (class highlight, for a page to style or animate); add (j, i) to light both ends. The default lights nothing.

Returns:

A self-contained SVG document (no external dependencies or references). SVG is a str subclass, so it behaves as the markup everywhere a string is expected, and additionally renders inline in notebooks and doc pages.

Return type:

SVG

Raises:
  • TypeError – If structure is not a GraphStructure, such as a TemporalStructure.

  • ValueError – If positions misses a node or places one outside the unit square, engine is unknown, or highlight names a pair (i, j) with i == j or no edge between i and j.

  • ImportError – If a Graphviz engine is named and pygraphviz is not installed.

Examples

>>> import numpy as np
>>> import andrey
>>> import andrey.viz
>>> rng = np.random.default_rng(0)
>>> x = rng.standard_normal(500)
>>> y = x + 0.5 * rng.standard_normal(500)
>>> out = andrey.pc(np.column_stack([x, y]))
>>> svg = andrey.viz.draw(out.structure, dark=True)
>>> svg.startswith("<svg")
True