NetworKit Visualization Tutorial

The vizbridges module provides the widgetFromGraph function, which creates and returns Python widgets for graph visualization. Per default, a graph is plotted in 2D using the Python-package ipycytoscape. If the parameter dimension to 3D, the graph network is plotted in 3D using plotly. For this to work one or both of the packages have to be installed on the machine, where the Jupyter backend is running. The default mode is 2D. There is an optional parameter for node scores or a partition list (e.g. as a result of from centrality or community detection algorithms). If provided the nodes are colored according to their partition membership or score.

Visualize in 2D using Cytoscape

When plotting a graph in 2D with Cytoscape, the internal layouting algorithm from Cytoscape is used. This and the performance of the plugin makes this visualization suitable for graphs with up to around 500 nodes. For larger graphs, it is recommended to use the 3D visualization.

[1]:
import networkit as nk
from networkit import vizbridges

# Read graph
G = nk.readGraph("../input/karate.graph", nk.Format.METIS)
# Initalize and run Betweenness algorithm
btwn = nk.centrality.Betweenness(G)
btwn.run()

# Visualize the karate graph with Betweenness scores
nk.vizbridges.widgetFromGraph(G, nodeScores = btwn.scores())
[1]:
[2]:
# Initalize and run PLM community detection algorithm
plm = nk.community.PLM(G)
plm.run()

# Visualize the karate graph with community detection
nk.vizbridges.widgetFromGraph(G, nodePartition = plm.getPartition())
[2]:

Visualize in 3D using Plotly

When plotting a graph in 3D with Plotly, the Maxent-Stress layouting from networkit.viz.MaxentStress is used. With a moderate to decent client, graph visualizations with up to 50k of nodes are possible. Note: The time it takes for generating the widget scales with the number of nodes.

[3]:
# Read graph
G = nk.readGraph("../input/karate.graph", nk.Format.METIS)
# Initalize and run Betweenness algorithm
btwn = nk.centrality.Betweenness(G)
btwn.run()

# Visualize the karate graph with Betweenness scores
nk.vizbridges.widgetFromGraph(G, dimension = nk.vizbridges.Dimension.Three, nodeScores = btwn.scores())
[3]:
[4]:
# Initalize and run PLM community detection algorithm
plm = nk.community.PLM(G)
plm.run()

# Visualize the power graph with community detection
nk.vizbridges.widgetFromGraph(G, dimension = nk.vizbridges.Dimension.Three, nodePartition = plm.getPartition())
[4]:

Visualize Edge Attributes

When plotting with Plotly (either 2D or 3D), there is the additional possibility of labeling the edges using EdgeAttributes as descriped in the Graph.ipynb. The following provides an example use case.

[5]:
import random
# Read graph
G = nk.readGraph("../input/tiny_01.graph", nk.Format.METIS)

G.indexEdges()

# Create new edge attributes named "myFloat" and "myInt"
att1 = G.attachEdgeAttribute("myFloat", float)
att2 = G.attachEdgeAttribute("myInt", int)

# Assign some random values to each edge
for u,v in G.iterEdges():
    att1[u,v] = round(random.uniform(0., 1.),2)
    att2[u,v] = random.randint(0, 10)

# Visualize edge attributes in 2D
nk.vizbridges.widgetFromGraph(G, dimension = nk.vizbridges.Dimension.TwoForcePlotly, edgeAttributes=[("myFloat", float), ("myInt", int)])
[5]:
[6]:
# Visualize edge attributes in 3D
nk.vizbridges.widgetFromGraph(G, dimension = nk.vizbridges.Dimension.Three, edgeAttributes=[("myFloat", float), ("myInt", int)])
[6]: