"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connected to server at http://public.lightning-viz.org\n"
]
},
{
"data": {
"application/javascript": [
"(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error(\"Cannot find module '\"+o+\"'\")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lgn = Lightning(ipython=True, host='http://public.lightning-viz.org')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random spatial graphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Spatial graphs have nodes with fixed spatial positions, and links between them.\n",
" \n",
"They are useful for spatial network data, where position has meaning (unlike force-directed graphs, where position is used dynamically during rendering).\n",
" \n",
"First we'll generate a random geometric graph using `networkx` and plot with basic styling.\n",
" \n",
"The random geometric graph places links between nodes with probability that depends on their spatial distance."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"G = nx.random_geometric_graph(100, 0.2)\n",
"pos = asarray(nx.get_node_attributes(G, 'pos').values())\n",
"mat = nx.adjacency_matrix(G).todense()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lgn.graph(pos[:,0], pos[:,1], mat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can add a color to each node. Here we color the same graph based on distance from the origin."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in pos]\n",
"lgn.graph(pos[:,0], pos[:,1], mat, values=dists, colormap='Greens')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As with other plots, we can also color using group labels.\n",
" \n",
"Here we assign a label to each point based on the shortest path from it to the center."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"center = argmin(dists)\n",
"p = nx.single_source_shortest_path_length(G, center)\n",
"xy = asarray([pos[i,:] for i in p.keys()])\n",
"g = p.values()\n",
"lgn.graph(xy[:,0], xy[:,1], mat, group=g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Edge bundling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Graphs with many edges can become hard to visualize (due to hairballs).\n",
" \n",
"Lightning helps with this by letting you click on points and see only the links to that node. Try it!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"G = nx.random_geometric_graph(50, 0.5)\n",
"pos = asarray(nx.get_node_attributes(G, 'pos').values())\n",
"dists = [(x - 0.5)**2 + (y - 0.5)**2 for x, y in pos]\n",
"mat = nx.adjacency_matrix(G).todense()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lgn.graph(pos[:,0], pos[:,1], mat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another option is to bundle edges together using an algorithm by Holton and Van Wijk, emphasizing large tracts.\n",
"See this [link](https://github.com/upphiminn/d3.ForceBundle) for the implementaiton.\n",
" \n",
"As with the unbundled version, you can click on points to highlight links."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"