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, whereu
is a NetworkX node andd
is its attribute dict. If it returnsNone
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 theformat
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
]) – Thebin
directory of the GraphViz installation. Defaults to theGRAPHVIZ_BIN
environment variable. If neither this parameter nor theGRAPHVIZ_BIN
environment variable is set, then nxv will try to autodetect thebin
directory of the GraphViz installation. This behavior is for convenience and should not be relied on in production settings.
- Return type
- Returns
If
format
is not an"ipython/*"
format, the render output; otherwise,None
.- Raises
GraphVizInstallationNotFoundError – If nxv cannot find a GraphViz installation.
GraphVizAlgorithmNotFoundError – If nxv cannot find the specified algorithm in a GraphViz installation.
GraphVizError – If GraphViz failed to run on the given inputs.
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
, andsubgraph
parameters.- Parameters
graph – An optional dict of GraphViz graph attributes, or a function
f(g, d)
that returns it, in whichg
is the NetworkX graph andd
is its attribute dict.node – An optional dict of GraphViz node attributes, or a function
f(u, d)
that returns it, in whichu
is a NetworkX node andd
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 andd
is its attribute dict. If styling a graph with multi-edges, the signature should bef(u, v, k, d)
instead, wherek
is the edge key.subgraph – An optional dict of GraphViz subgraph attributes, or a function
f(s)
that returns it, in whichs
is a subgraph key. This only applies when callingnxv.render
with asubgraph_func
.
-
nxv.
compose
(styles)¶
-
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 returnsapply(funcs[key(*args, **kwargs)], *args, **kwargs)
.If
key(*args, **kwargs)
is not infuncs
butdefault
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 returnsapply(funcs[key(*args, **kwargs)], *args, **kwargs)
.
-
nxv.styles.
font
(fontname=None, fontsize=None)¶ Styles text in a graph using the given 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 fromu
tov
.
- 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 beedge_key(u, v, k, d)
instead, wherek
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.