Network Visualization

With the rise of interest in network studies, many tools have been developed to visualize networks. Here I am going to introduce two of them, the Python NetworkX module and the D3.js based on Javascript.

Construct Network

We used the Bilateral Remittance Matrix 2012 download from here to construct a country-based network of money flows.

import random
from pylab import *
import numpy as np
import networkx as nx
import as cm

a = [line for line in open('/Users/csid/Desktop/BRM2012.txt','r')]
b = a[0].split('\r')
countries = b[1].split('\t')[1:-2]
G = nx.DiGraph()
for i in b[2:-8]:
    x = i.split('\t')[0]
    y = i.split('\t')[1:-2]
    for j in range(len(y)):
        if y[j] and float(y[j])>0:

Network Properties

Good visualizations tell stories. In visualizing networks we usually calculate network properties and integrate this information in the final output. Here I calculate the flow distance of each node from "source" and use it to cluster nodes.

H = flowBalancing(G)
L = flowDistanceFromSource(H)
T = H.out_degree(weight='weight')
pos = nx.spring_layout(G)
del L['sink']

The network G has 201 nodes and 7,660 edges.

dis = sorted(L.items(),key=lambda x:x[1])
x,y = np.array([pos[i] for i,l in dis]).T
t = np.array([T[i] for i,l in dis])
r = np.array([np.ceil(l) for i,l in dis])
cmap = cm.get_cmap('RdYlGn_r', int(max(r)))
fig = plt.figure(figsize=(10, 8),facecolor='white')
cax = scatter(x,y,s=np.log(t+1)*30,c=r,cmap=cmap,edgecolor='gray',alpha=1)
cbar = fig.colorbar(cax, ticks=[1, 2, 3, 4])
#savefig('/Users/csid/Desktop/test.png', bbox_inches='tight')

The above code gives this figure


Constrained by the power of browser engines, D3.js is not suitable for networks that contain more than thousands of edges and nodes. So we'd better select a part of the network.

g = nx.DiGraph()
for x,y in G.edges():
    w = G[x][y]['weight']
    if w > 100:

The above code give a new network which has 137 nodes and 548 edges.Now we write Javascript codes in Python.

nodes = [{"name" : i, "group" : int(L[i])} for i in g.nodes()]
maxw = max([G[x][y]['weight'] for x,y in g.edges()])
edges = [{ "source" : g.nodes().index(x), "target" : g.nodes().index(y),\
          "value" : g[x][y]['weight']/maxw} for x,y in g.edges()]

.link {
stroke: #666;
opacity: 0.9;
stroke-width: 1.5px;
.node circle {
stroke: #fff;
opacity: 0.9;
stroke-width: 1.5px;
.node:not(:hover) .nodetext {
display: none;
text {
font: 7px serif;
opacity: 0.9;
pointer-events: none;
<script src=></script>
 var links =

var nodes =

 var width = 800
height = 800;

var color = d3.scale.category10();

var force = d3.layout.force()
.size([width, height])
.on("tick", tick)

var svg ="body").append("svg")
.attr("width", width)
.attr("height", height);

var link = svg.selectAll(".link")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });

var node = svg.selectAll(".node")
.attr("class", "node")
.style("fill", function(d) { return color(; })
.style("opacity", 0.9)
.on("mouseover", mouseover)
.on("mouseout", mouseout)

.attr("r", 6)

.attr("class", "nodetext")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return });

function tick() {
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return; })
.attr("y2", function(d) { return; });

node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

function mouseover() {"circle").transition()
.attr("r", 16);"text").transition()
.attr("x", 13)
.style("stroke-width", ".5px")
.style("font", "17.5px serif")
.style("opacity", 1);

function mouseout() {"circle").transition()
.attr("r", 8);


open('/Users/csid/Desktop/moneyflow.html', "wb").write(a+str(edges)+b+str(nodes)+c)

The above codes create a .html file that gives the following interactive visualization:

results matching ""

    No results matching ""