Graphs

Visualization of graphs as SVG images.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor, load_netset
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels = graph.labels

Visualization

[5]:
image = svg_graph(adjacency, position, labels=labels, scale=3, node_size=30)
[6]:
SVG(image)
[6]:
../../_images/tutorials_visualization_graphs_9_0.svg

Export

[7]:
svg_graph(adjacency, position, labels=labels, filename='karate_club')
[7]:
'<svg width="440" height="340" xmlns="http://www.w3.org/2000/svg">\n<path stroke-width="1" stroke="black" d="M 264 200 353 168"/>\n<path stroke-width="1" stroke="black" d="M 20 116 153 125"/>\n<path stroke-width="1" stroke="black" d="M 28 91 131 68"/>\n<path stroke-width="1" stroke="black" d="M 28 91 20 116"/>\n<path stroke-width="1" stroke="black" d="M 28 91 153 125"/>\n<path stroke-width="1" stroke="black" d="M 162 20 171 45"/>\n<path stroke-width="1" stroke="black" d="M 162 20 246 82"/>\n<path stroke-width="1" stroke="black" d="M 122 103 251 143"/>\n<path stroke-width="1" stroke="black" d="M 122 103 131 68"/>\n<path stroke-width="1" stroke="black" d="M 122 103 20 116"/>\n<path stroke-width="1" stroke="black" d="M 122 103 246 82"/>\n<path stroke-width="1" stroke="black" d="M 197 120 251 143"/>\n<path stroke-width="1" stroke="black" d="M 197 120 153 125"/>\n<path stroke-width="1" stroke="black" d="M 197 120 246 82"/>\n<path stroke-width="1" stroke="black" d="M 171 45 131 68"/>\n<path stroke-width="1" stroke="black" d="M 171 45 162 20"/>\n<path stroke-width="1" stroke="black" d="M 20 116 122 103"/>\n<path stroke-width="1" stroke="black" d="M 20 116 28 91"/>\n<path stroke-width="1" stroke="black" d="M 131 68 246 82"/>\n<path stroke-width="1" stroke="black" d="M 131 68 260 73"/>\n<path stroke-width="1" stroke="black" d="M 388 216 264 200"/>\n<path stroke-width="1" stroke="black" d="M 388 216 353 168"/>\n<path stroke-width="1" stroke="black" d="M 313 34 260 73"/>\n<path stroke-width="1" stroke="black" d="M 313 34 246 82"/>\n<path stroke-width="1" stroke="black" d="M 331 143 264 200"/>\n<path stroke-width="1" stroke="black" d="M 331 143 353 168"/>\n<path stroke-width="1" stroke="black" d="M 331 143 246 82"/>\n<path stroke-width="1" stroke="black" d="M 171 45 260 73"/>\n<path stroke-width="1" stroke="black" d="M 366 61 260 73"/>\n<path stroke-width="1" stroke="black" d="M 420 202 264 200"/>\n<path stroke-width="1" stroke="black" d="M 420 202 353 168"/>\n<path stroke-width="1" stroke="black" d="M 228 32 260 73"/>\n<path stroke-width="1" stroke="black" d="M 228 32 246 82"/>\n<path stroke-width="1" stroke="black" d="M 131 68 28 91"/>\n<path stroke-width="1" stroke="black" d="M 131 68 122 103"/>\n<path stroke-width="1" stroke="black" d="M 131 68 171 45"/>\n<path stroke-width="1" stroke="black" d="M 366 61 246 82"/>\n<path stroke-width="1" stroke="black" d="M 220 320 264 277"/>\n<path stroke-width="1" stroke="black" d="M 171 45 246 82"/>\n<path stroke-width="1" stroke="black" d="M 344 107 282 125"/>\n<path stroke-width="1" stroke="black" d="M 246 82 282 125"/>\n<path stroke-width="1" stroke="black" d="M 246 82 197 100"/>\n<path stroke-width="1" stroke="black" d="M 246 82 282 153"/>\n<path stroke-width="1" stroke="black" d="M 246 82 348 43"/>\n<path stroke-width="1" stroke="black" d="M 246 82 273 27"/>\n<path stroke-width="1" stroke="black" d="M 246 82 313 34"/>\n<path stroke-width="1" stroke="black" d="M 246 82 331 143"/>\n<path stroke-width="1" stroke="black" d="M 246 82 366 61"/>\n<path stroke-width="1" stroke="black" d="M 246 82 228 32"/>\n<path stroke-width="1" stroke="black" d="M 246 82 131 68"/>\n<path stroke-width="1" stroke="black" d="M 246 82 162 20"/>\n<path stroke-width="1" stroke="black" d="M 246 82 122 103"/>\n<path stroke-width="1" stroke="black" d="M 246 82 197 120"/>\n<path stroke-width="1" stroke="black" d="M 246 82 171 45"/>\n<path stroke-width="1" stroke="black" d="M 246 82 344 107"/>\n<path stroke-width="1" stroke="black" d="M 260 73 246 82"/>\n<path stroke-width="1" stroke="black" d="M 260 73 153 125"/>\n<path stroke-width="1" stroke="black" d="M 260 73 344 107"/>\n<path stroke-width="1" stroke="black" d="M 260 73 171 45"/>\n<path stroke-width="1" stroke="black" d="M 344 107 260 73"/>\n<path stroke-width="1" stroke="black" d="M 344 107 246 82"/>\n<path stroke-width="1" stroke="black" d="M 153 125 264 200"/>\n<path stroke-width="1" stroke="black" d="M 153 125 20 116"/>\n<path stroke-width="1" stroke="black" d="M 153 125 28 91"/>\n<path stroke-width="1" stroke="black" d="M 153 125 197 120"/>\n<path stroke-width="1" stroke="black" d="M 153 125 260 73"/>\n<path stroke-width="1" stroke="black" d="M 344 107 353 168"/>\n<path stroke-width="1" stroke="black" d="M 153 125 246 82"/>\n<path stroke-width="1" stroke="black" d="M 260 73 282 125"/>\n<path stroke-width="1" stroke="black" d="M 260 73 348 43"/>\n<path stroke-width="1" stroke="black" d="M 260 73 273 27"/>\n<path stroke-width="1" stroke="black" d="M 260 73 313 34"/>\n<path stroke-width="1" stroke="black" d="M 260 73 366 61"/>\n<path stroke-width="1" stroke="black" d="M 260 73 228 32"/>\n<path stroke-width="1" stroke="black" d="M 260 73 131 68"/>\n<path stroke-width="1" stroke="black" d="M 260 73 251 143"/>\n<path stroke-width="1" stroke="black" d="M 220 320 211 275"/>\n<path stroke-width="1" stroke="black" d="M 273 27 246 82"/>\n<path stroke-width="1" stroke="black" d="M 273 27 260 73"/>\n<path stroke-width="1" stroke="black" d="M 353 168 388 216"/>\n<path stroke-width="1" stroke="black" d="M 353 168 331 143"/>\n<path stroke-width="1" stroke="black" d="M 353 168 420 202"/>\n<path stroke-width="1" stroke="black" d="M 353 168 344 107"/>\n<path stroke-width="1" stroke="black" d="M 251 143 264 200"/>\n<path stroke-width="1" stroke="black" d="M 251 143 353 168"/>\n<path stroke-width="1" stroke="black" d="M 251 143 304 191"/>\n<path stroke-width="1" stroke="black" d="M 251 143 340 186"/>\n<path stroke-width="1" stroke="black" d="M 251 143 282 125"/>\n<path stroke-width="1" stroke="black" d="M 251 143 197 100"/>\n<path stroke-width="1" stroke="black" d="M 251 143 282 153"/>\n<path stroke-width="1" stroke="black" d="M 251 143 122 103"/>\n<path stroke-width="1" stroke="black" d="M 251 143 197 120"/>\n<path stroke-width="1" stroke="black" d="M 251 143 260 73"/>\n<path stroke-width="1" stroke="black" d="M 304 191 264 200"/>\n<path stroke-width="1" stroke="black" d="M 353 168 282 153"/>\n<path stroke-width="1" stroke="black" d="M 353 168 340 186"/>\n<path stroke-width="1" stroke="black" d="M 353 168 304 191"/>\n<path stroke-width="1" stroke="black" d="M 353 168 251 143"/>\n<path stroke-width="1" stroke="black" d="M 264 200 251 143"/>\n<path stroke-width="1" stroke="black" d="M 264 200 304 191"/>\n<path stroke-width="1" stroke="black" d="M 264 200 255 255"/>\n<path stroke-width="1" stroke="black" d="M 264 200 211 275"/>\n<path stroke-width="1" stroke="black" d="M 264 200 264 277"/>\n<path stroke-width="1" stroke="black" d="M 264 200 340 186"/>\n<path stroke-width="1" stroke="black" d="M 264 200 282 125"/>\n<path stroke-width="1" stroke="black" d="M 304 191 353 168"/>\n<path stroke-width="1" stroke="black" d="M 264 200 188 252"/>\n<path stroke-width="1" stroke="black" d="M 264 200 335 236"/>\n<path stroke-width="1" stroke="black" d="M 264 200 282 153"/>\n<path stroke-width="1" stroke="black" d="M 264 200 388 216"/>\n<path stroke-width="1" stroke="black" d="M 264 200 331 143"/>\n<path stroke-width="1" stroke="black" d="M 264 200 420 202"/>\n<path stroke-width="1" stroke="black" d="M 264 200 153 125"/>\n<path stroke-width="1" stroke="black" d="M 353 168 264 200"/>\n<path stroke-width="1" stroke="black" d="M 264 200 122 212"/>\n<path stroke-width="1" stroke="black" d="M 304 191 251 143"/>\n<path stroke-width="1" stroke="black" d="M 304 191 340 186"/>\n<path stroke-width="1" stroke="black" d="M 304 191 335 236"/>\n<path stroke-width="1" stroke="black" d="M 282 125 246 82"/>\n<path stroke-width="1" stroke="black" d="M 197 100 251 143"/>\n<path stroke-width="1" stroke="black" d="M 197 100 246 82"/>\n<path stroke-width="1" stroke="black" d="M 188 252 264 200"/>\n<path stroke-width="1" stroke="black" d="M 188 252 255 255"/>\n<path stroke-width="1" stroke="black" d="M 188 252 211 275"/>\n<path stroke-width="1" stroke="black" d="M 122 212 264 200"/>\n<path stroke-width="1" stroke="black" d="M 282 125 260 73"/>\n<path stroke-width="1" stroke="black" d="M 335 236 264 200"/>\n<path stroke-width="1" stroke="black" d="M 282 153 264 200"/>\n<path stroke-width="1" stroke="black" d="M 282 153 353 168"/>\n<path stroke-width="1" stroke="black" d="M 282 153 251 143"/>\n<path stroke-width="1" stroke="black" d="M 282 153 304 191"/>\n<path stroke-width="1" stroke="black" d="M 282 153 246 82"/>\n<path stroke-width="1" stroke="black" d="M 348 43 260 73"/>\n<path stroke-width="1" stroke="black" d="M 348 43 246 82"/>\n<path stroke-width="1" stroke="black" d="M 335 236 304 191"/>\n<path stroke-width="1" stroke="black" d="M 246 82 153 125"/>\n<path stroke-width="1" stroke="black" d="M 282 125 344 107"/>\n<path stroke-width="1" stroke="black" d="M 282 125 264 200"/>\n<path stroke-width="1" stroke="black" d="M 304 191 282 153"/>\n<path stroke-width="1" stroke="black" d="M 255 255 264 200"/>\n<path stroke-width="1" stroke="black" d="M 255 255 264 277"/>\n<path stroke-width="1" stroke="black" d="M 255 255 188 252"/>\n<path stroke-width="1" stroke="black" d="M 211 275 264 200"/>\n<path stroke-width="1" stroke="black" d="M 211 275 264 277"/>\n<path stroke-width="1" stroke="black" d="M 211 275 188 252"/>\n<path stroke-width="1" stroke="black" d="M 282 125 251 143"/>\n<path stroke-width="1" stroke="black" d="M 211 275 220 320"/>\n<path stroke-width="1" stroke="black" d="M 264 277 255 255"/>\n<path stroke-width="1" stroke="black" d="M 264 277 211 275"/>\n<path stroke-width="1" stroke="black" d="M 264 277 220 320"/>\n<path stroke-width="1" stroke="black" d="M 340 186 264 200"/>\n<path stroke-width="1" stroke="black" d="M 340 186 353 168"/>\n<path stroke-width="1" stroke="black" d="M 340 186 251 143"/>\n<path stroke-width="1" stroke="black" d="M 340 186 304 191"/>\n<path stroke-width="1" stroke="black" d="M 264 277 264 200"/>\n<path stroke-width="1" stroke="black" d="M 246 82 260 73"/>\n<circle cx="264" cy="200" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="353" cy="168" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="251" cy="143" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="304" cy="191" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="255" cy="255" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="211" cy="275" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="264" cy="277" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="340" cy="186" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="282" cy="125" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="197" cy="100" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="188" cy="252" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="122" cy="212" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="335" cy="236" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="282" cy="153" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="348" cy="43" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="273" cy="27" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="220" cy="320" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="388" cy="216" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="313" cy="34" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="331" cy="143" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="366" cy="61" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="420" cy="202" r="7.0" style="fill:red;stroke:black;stroke-width:1.0"/>\n<circle cx="228" cy="32" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="131" cy="68" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="20" cy="116" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="28" cy="91" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="162" cy="20" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="122" cy="103" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="197" cy="120" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="171" cy="45" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="344" cy="107" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="153" cy="125" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="260" cy="73" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n<circle cx="246" cy="82" r="7.0" style="fill:blue;stroke:black;stroke-width:1.0"/>\n</svg>\n'

Degrees

[8]:
image = svg_graph(adjacency, position, labels=labels, display_node_weight=True)
[9]:
SVG(image)
[9]:
../../_images/tutorials_visualization_graphs_14_0.svg

Scores

[10]:
degrees = adjacency.dot(np.ones(adjacency.shape[0]))
[11]:
image = svg_graph(adjacency, position, scores=degrees)
[12]:
SVG(image)
[12]:
../../_images/tutorials_visualization_graphs_18_0.svg

Seeds

[13]:
seeds = list(np.argsort(-degrees)[:2])
[14]:
image = svg_graph(adjacency, position, labels=labels, seeds=seeds)
[15]:
SVG(image)
[15]:
../../_images/tutorials_visualization_graphs_22_0.svg

No edges

[16]:
graph = load_netset('openflights')
adjacency = graph.adjacency
position = graph.position
[17]:
weights = adjacency.dot(np.ones(adjacency.shape[0]))
[18]:
image = svg_graph(adjacency, position, scores=np.log(weights), node_order=np.argsort(weights),
                  node_size_min=2, node_size_max=10, height=400, width=800,
                  display_node_weight=True, display_edges=False)
[19]:
SVG(image)
[19]:
../../_images/tutorials_visualization_graphs_27_0.svg

Digraphs

[20]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[21]:
image = svg_digraph(adjacency, position, names)
[22]:
SVG(image)
[22]:
../../_images/tutorials_visualization_graphs_31_0.svg

Bigraphs

[23]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[24]:
# default layout
image = svg_bigraph(biadjacency, names_row, names_col, color_row='blue', color_col='red')
[25]:
SVG(image)
[25]:
../../_images/tutorials_visualization_graphs_35_0.svg
[26]:
# keep original order of rows and columns
image = svg_bigraph(biadjacency, names_row, names_col=names_col, color_row='blue', color_col='red',
                    reorder=False)
[27]:
SVG(image)
[27]:
../../_images/tutorials_visualization_graphs_37_0.svg