import os
from lightning import Lightning
from numpy import random, asarray, argmin
from colorsys import hsv_to_rgb
import networkx as nx
lgn = Lightning(ipython=True, host='http://public.lightning-viz.org')
Connected to server at http://public.lightning-viz.org
Spatial graphs have nodes with fixed spatial positions, and links between them.
G = nx.random_geometric_graph(100, 0.2)
pos = asarray(nx.get_node_attributes(G, 'pos').values())
mat = nx.adjacency_matrix(G).todense()
lgn.graph(pos[:,0], pos[:,1], mat)
We can add a color to each node. Here we color the same graph based on distance from the origin.
dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in pos]
lgn.graph(pos[:,0], pos[:,1], mat, values=dists, colormap='Greens')
As with other plots, we can also color using group labels.
center = argmin(dists)
p = nx.single_source_shortest_path_length(G, center)
xy = asarray([pos[i,:] for i in p.keys()])
g = p.values()
lgn.graph(xy[:,0], xy[:,1], mat, group=g)
Graphs with many edges can become hard to visualize (due to hairballs).
G = nx.random_geometric_graph(50, 0.5)
pos = asarray(nx.get_node_attributes(G, 'pos').values())
dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in pos]
mat = nx.adjacency_matrix(G).todense()
lgn.graph(pos[:,0], pos[:,1], mat)
Another option is to bundle edges together using an algorithm by Holton and Van Wijk, emphasizing large tracts. See this link for the implementaiton.
lgn.graphbundled(pos[:,0], pos[:,1], mat)