Reference

Rendering

nxv.render(graph, style=None, *, algorithm=None, format=None, graphviz_bin=None, subgraph_func=None)

Render a NetworkX graph using GraphViz.

In a Jupyter notebook, this will automatically display as an SVG.

Parameters
  • graph (Union[Graph, DiGraph, MultiGraph, MultiDiGraph]) – A NetworkX graph.

  • style (Optional[Style]) – A style specifying how graph nodes and edges should map to GraphViz attributes.

  • subgraph_func – An optional function f(u, d) that returns a subgraph key, where u is a NetworkX node and d is its attribute dict. If it returns None the node is not in any subgraph.

  • algorithm (Optional[str]) – The GraphViz layout algorithm. Valid options include "circo", "dot", "fdp", "neato", "osage", "sfdp", "twopi". Defaults to "dot".

  • format (Optional[str]) – The GraphViz output format. Valid options include "svg" and "raw". In a Jupyter notebook, prefixing the format with "ipython/" will automatically display the rendered output. When running in an interactive setting like a Jupyter notebook, the default is "ipython/svg". Otherwise, this parameter is required.

  • graphviz_bin (Optional[str]) – The bin directory of the GraphViz installation. Defaults to the GRAPHVIZ_BIN environment variable. If neither this parameter nor the GRAPHVIZ_BIN environment variable is set, then nxv will try to autodetect the bin directory of the GraphViz installation. This behavior is for convenience and should not be relied on in production settings.

Return type

Optional[bytes]

Returns

If format is not an "ipython/*" format, the render output; otherwise, None.

Raises

Styling

class nxv.Style(*, graph=None, node=None, edge=None, subgraph=None)

A specification for how to style a NetworkX graph using GraphViz.

See the GraphViz attributes documentation for information on what attributes are available to use with the graph, node, edge, and subgraph parameters.

Parameters
  • graph – An optional dict of GraphViz graph attributes, or a function f(g, d) that returns it, in which g is the NetworkX graph and d is its attribute dict.

  • node – An optional dict of GraphViz node attributes, or a function f(u, d) that returns it, in which u is a NetworkX node and d is its attribute dict.

  • edge – An optional dict of GraphViz edge attributes, or a function f(u, v, d) that returns it, in which (u, v) is a NetworkX edge and d is its attribute dict. If styling a graph with multi-edges, the signature should be f(u, v, k, d) instead, where k is the edge key.

  • subgraph – An optional dict of GraphViz subgraph attributes, or a function f(s) that returns it, in which s is a subgraph key. This only applies when calling nxv.render with a subgraph_func.

nxv.compose(styles)

Compose a sequence of Style objects as a single Style.

Parameters

styles (Iterable[Optional[Style]]) – An iterable of Style objects.

Return type

Style

Returns

The composed Style.

nxv.chain(funcs)

Chain a sequence of dict-returning functions together to form a new dict-returning function.

The result is a function f(*args, **kwargs) that returns {**apply(funcs[0], *args, **kwargs), **apply(funcs[1], *args, **kwargs), ...}.

Parameters

funcs – An iterable of functions that return dicts.

Returns

A function f(*args, **kwargs) that returns {**apply(funcs[0], *args, **kwargs), **apply(funcs[1], *args, **kwargs), ...}.

nxv.switch(key, funcs, *, default=None)

Combine a dict of keyed functions to form a new function.

The result is a function f(*args, **kwargs) that returns apply(funcs[key(*args, **kwargs)], *args, **kwargs).

If key(*args, **kwargs) is not in funcs but default is present, apply(default, *args, **kwargs) will be returned instead.

Parameters
  • key – The key selector function.

  • funcs – The mapping from keys to functions.

  • default – An optional default function for keys that do not appear in funcs.

Returns

The function f(*args, **kwargs) that returns apply(funcs[key(*args, **kwargs)], *args, **kwargs).

nxv.styles.verbose()

Get a verbose Style that shows all of the data in a graph.

Return type

Style

Returns

A verbose Style.

nxv.styles.font(fontname=None, fontsize=None)

Styles text in a graph using the given font.

Parameters
Return type

Style

Returns

A Style object that applies this font.

HTML-Like Labels

The nxv.html_like subpackage provides functions for building GraphViz HTML-like labels.

The idiomatic import for this subpackage is:

import nxv.html_like as H
nxv.html_like.join(children)
nxv.html_like.line_break(attributes=None)
nxv.html_like.font(content, attributes=None)
nxv.html_like.italic(content)
nxv.html_like.bold(content)
nxv.html_like.underline(content)
nxv.html_like.overline(content)
nxv.html_like.subscript(content)
nxv.html_like.superscript(content)
nxv.html_like.strikethrough(content)
nxv.html_like.table(rows, attributes=None)
nxv.html_like.table_row(cells)
nxv.html_like.horizontal_rule()
nxv.html_like.table_cell(content, attributes=None)
nxv.html_like.vertical_rule()
nxv.html_like.image(attributes=None)

Utilities

nxv.neighborhood(graph, nodes, *, radius=None, cost=None)

Get the subgraph in the neighborhood of the specified nodes.

This is useful for viewing a small portion of a large graph.

Parameters
  • graph – A graph.

  • nodes – An iterable of nodes.

  • radius – The size of the neighborhood.

  • cost – A function f(u, v) specifying the cost of traversing from u to v.

Returns

The neighborhood subgraph.

nxv.boundary(graph, subgraph)

Get the nodes in the subgraph that have neighbors in the graph but not in the subgraph.

This is useful for conditionally styling nodes at the boundary of a subgraph. For example:

boundary = nxv.boundary(graph, subgraph)
style = nxv.Style(node=lambda u, d: {
    'style': 'dashed' if u in boundary else 'solid',
})
nxv.render(subgraph, style)
Parameters
  • graph – A graph.

  • subgraph – A subgraph of the graph.

Returns

The nodes in the subgraph that have neighbors in the graph but not in the subgraph.

nxv.to_ordered_graph(graph, node_key=None, edge_key=None, attr_key=None)

Create an ordered copy of the specified graph, with nodes and edges ordered by the specified key functions.

Parameters
  • graph – The graph to order.

  • node_key – The node key function, node_key(u, d). Defaults to the identity function.

  • edge_key – The edge key function, edge_key(u, v, d). If the graph has multi-edges, the signature should be edge_key(u, v, k, d) instead, where k is the edge key. Defaults to the identity function.

  • attr_key – The attribute key function, attr_key(k, v). Defaults to the identity function.

Returns

A copy of the graph with the nodes and edges ordered by the specified key functions.

nxv.contrasting_color(channels, *, options=None)

Get a color that most contrasts with a specified color.

Parameters
  • channels – The RGB or RGBA color channels. Values should be in the range [0, 1].

  • options – The possible contrasting colors. Defaults to black and white.

Returns

The color option that most contrasts the input color.

Errors

class nxv.GraphVizInstallationNotFoundError

Raised when a GraphViz installation is not found.

class nxv.GraphVizAlgorithmNotFoundError

Raised when a GraphViz algorithm is not found.

class nxv.GraphVizError

Raised when a GraphViz run fails.