Quickstart¶
nxv renders NetworkX graphs using GraphViz.
Using nxv inside of Jupyter is the easiest and recommended way to get started.
Using nxv outside of Jupyter describes how to use nxv in other settings.
Using nxv inside of Jupyter¶
Start by importing networkx and nxv.
import networkx as nx
import nxv
Define a simple NetworkX graph.
graph = nx.Graph()
graph.add_edge("A", "B")
graph.add_edge("B", "C")
graph.add_edge("C", "D")
graph.add_edge("B", "E")
Render the graph with GraphViz using the render()
function.
nxv.render(graph)
Use a Style
to specify how to style the graph using GraphViz.
style = nxv.Style(
graph={"rankdir": "LR"},
node={"shape": "square"},
edge={"style": "dashed"},
)
See the GraphViz attributes documentation for information on what attributes are available to use.
Render the graph with the Style
by passing it to the render()
function.
nxv.render(graph, style)
The Style
parameters can be functions that map the parts of a graph to different styles.
style = nxv.Style(
graph={"rankdir": "LR"},
node=lambda u, d: {"shape": "circle" if u in "AEIOU" else "square"},
edge=lambda u, v, d: {"style": "dashed", "label": u + v},
)
nxv.render(graph, style)
Using nxv outside of Jupyter¶
Outside of Jupyter, the format
parameter of the render()
function is required.
When the format
parameter is provided, the behavior of the render()
function is to
return the bytes
of the result in the specified format.
data = nxv.render(graph, style, format="svg")
with open("graph.svg", "wb") as f:
f.write(data)