In [1]:
import os
from lightning import Lightning
from numpy import random, asarray, corrcoef, argsort, array
import networkx as nx
from sklearn import datasets
In [2]:
# replace with your own host and credentials, e.g. http://localhost:3000 or http://my-lightning-server.herokuapp.com
host = 'http://lightning-docs.herokuapp.com'
auth = (os.getenv('LIGHTNING_USERNAME'), os.getenv('LIGHTNING_PASSWORD'))
In [3]:
lgn = Lightning(ipython=True, host=host, auth=auth)
lgn.create_session('matrix-ipython');
Matrices are useful ways to visualize dense tables and correlation matrices data.
First we show a random matrix with default styles.
Matrices are rendered as leaflet maps, so you can pan and zoom around them.
In [4]:
mat = random.randn(10,10)
lgn.matrix(mat)
Out[4]:
Rectanglular matrices will automatically fill the same square space.
In [5]:
mat = random.randn(10,20)
lgn.matrix(mat)
Out[5]:
In [6]:
mat = random.randn(20,10)
lgn.matrix(mat)
Out[6]:
Matrices can be rendered using any colorbrewer colormaps.
In [7]:
mat = random.rand(10,10)
lgn.matrix(mat, colormap='Reds')
Out[7]:
In [8]:
mat = random.rand(10,10)
lgn.matrix(mat, colormap='Spectral')
Out[8]:
Matrices are also commonly used to look at correlation structure.
In [9]:
d, l = datasets.make_blobs(n_features=5, n_samples=25, centers=5, cluster_std=2.0, random_state=100)
d = d[argsort(l)]
c = corrcoef(d)
lgn.matrix(c)
Out[9]:
Closely realted to correlation matrices are adjacency matrices, in which the rows (and columns) represent nodes and the value in each cell indicates a connection.
Lightning offers a separate adjacency
visualization type. Here is one for a random graph with community structure.
In [20]:
s = [20, 10, 10, 5, 5]
n = sum(s)
G = nx.random_partition_graph(s, 0.5, 0.01)
mat = nx.adjacency_matrix(G).todense()
lgn.adjacency(mat)
Out[20]:
If we assign each graph partition a label, we can color nodes by their label.
In [21]:
def label(G, n):
for i, p in enumerate(G.graph['partition']):
if n in p:
return i
l = [label(G, x) for x in range(0,50)]
lgn.adjacency(mat, label=l)
Out[21]:
You can use Lightning to look at a force-directed graph for the same network!
In [22]:
lgn.force(mat, label=l)
Out[22]: