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_typesflag 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 whenpaletteis set.palette (
Palette|None) – APalette(andrey.viz.LIGHT/DARKor a custom one); overridesdark.width (
int|None) – Canvas width in pixels.Nonesizes 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;Nonesizes 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. SetFalsefor 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,ydownward, aslayout()returns.Noneuseslayout()withengineandengine_options.engine (
str) – The layoutlayout()runs whenpositionsisNone:"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’saccentcolor.(i, j)lights the mark at nodeion the edge between nodesiandj, and that half of the edge, with a soft glow under it (classhighlight, 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).
SVGis astrsubclass, so it behaves as the markup everywhere a string is expected, and additionally renders inline in notebooks and doc pages.- Return type:
- Raises:
TypeError – If
structureis not aGraphStructure, such as aTemporalStructure.ValueError – If
positionsmisses a node or places one outside the unit square,engineis unknown, orhighlightnames a pair(i, j)withi == jor no edge betweeniandj.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