Force-directed graphs in Lightning


In [1]:
import os
from lightning import Lightning

from numpy import random, asarray, linspace, corrcoef
from colorsys import hsv_to_rgb
from sklearn import datasets
import networkx as nx

Connect to server

In [2]:
lgn = Lightning(ipython=True, host='')
Lightning initialized
Connected to server at

Random binary network

A random graph where every node has the same degree, with default styling.
This and other graph plots take matrices (e.g. adjacency matrices) as inputs.
We use the networkx library to generate the graph, then get its adjancency matrix.
Double click on a point to see its neighbors (all should have 3).
Drag points to try and move the graph, it should remain tight because of the degree structure.

In [3]:
n = 100
G = nx.random_regular_graph(3,n)
mat = nx.adjacency_matrix(G).todense()

Random weighted network

The previous network was binary (all links either 0 or 1). Here the links are weighted, which is reflected in the line widths.

In [4]:
G = nx.random_regular_graph(3,100)
mat = asarray(nx.adjacency_matrix(G).todense()) * (((random.rand(100,100))*5) ** 2)

Lobster network

The lobster graph, a backbone with some leaves, is colored here by node ordering.
We also set all nodes with degree less than 2 to gray.

In [5]:
G = nx.random_lobster(60, 0.5, 0.0, seed=44)
mat = nx.adjacency_matrix(G).todense()
n = len(G.nodes())
c = [list(asarray(hsv_to_rgb(float(x) / n , 0.8, 1.0))*255) if y > 1 else [150,150,150] for (x,y) in G.degree_iter()]
lgn.force(mat, color=c)

Coloring by degree

Here we color points (and also change their size) to indicate their degree.
Click to confirm bigger points have more connections.

In [6]:
G = nx.random_geometric_graph(50, 0.1)
mat = nx.adjacency_matrix(G).todense()
g =
lgn.force(mat, group=g, size=(asarray(g) + 1.5)*3)

Graph of clustering

Graphs can be a useful way to look at data that doesn't neccessarily come from a graph.
Here we create a graph from a thresholded correlation matrix on data drawn from a set of clusters.
The cluster identities are clear in the resulting graph.

In [7]:
d, g = datasets.make_blobs(n_features=5, n_samples=50, centers=5, cluster_std=2.0, random_state=100)
c = corrcoef(d)
c[c<0.9] = 0
lgn.force(c, group=g)