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_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels = graph.labels
[5]:
# graph
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
[5]:
../../_images/tutorials_visualization_graphs_7_0.svg
[6]:
# export
image = svg_graph(adjacency, position, labels=labels, filename='karate_club')
[7]:
# adding names
image = svg_graph(adjacency, position, names=np.arange(34), name_position='below', labels=labels)
SVG(image)
[7]:
../../_images/tutorials_visualization_graphs_9_0.svg
[8]:
# node size
image = svg_graph(adjacency, position, labels=labels, display_node_weight=True)
SVG(image)
[8]:
../../_images/tutorials_visualization_graphs_10_0.svg
[9]:
# scores (here = degrees)
degrees = adjacency.dot(np.ones(adjacency.shape[0]))
image = svg_graph(adjacency, position, scores=degrees)
SVG(image)
[9]:
../../_images/tutorials_visualization_graphs_11_0.svg
[10]:
# seeds (here 2 nodes of highest degrees)
seeds = list(np.argsort(-degrees)[:2])
image = svg_graph(adjacency, position, labels=labels, seeds=seeds)
SVG(image)
[10]:
../../_images/tutorials_visualization_graphs_12_0.svg
[11]:
# no edge
graph = load_netset('openflights')
adjacency = graph.adjacency
position = graph.position

Parsing files...
Done.
[12]:
weights = adjacency.dot(np.ones(adjacency.shape[0]))
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)
SVG(image)
[12]:
../../_images/tutorials_visualization_graphs_14_0.svg

Directed graphs

[13]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[14]:
image = svg_graph(adjacency, position, names, name_position='above')
SVG(image)
[14]:
../../_images/tutorials_visualization_graphs_17_0.svg

Bipartite graphs

[15]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[16]:
# default layout
image = svg_bigraph(biadjacency, names_row, names_col, color_row='blue', color_col='red')
SVG(image)
[16]:
../../_images/tutorials_visualization_graphs_20_0.svg
[17]:
# 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)
SVG(image)
[17]:
../../_images/tutorials_visualization_graphs_21_0.svg