View on blocks
In constructing the matrix view I borrowed heavily from the Les Miserables example seen here.
The default ordering is set to be by name and will change its self periodically to be ordered by frequency and then back again.
The by name ordering of the matrix provides information if you are like myself and do not remember information about the states off hand.
When ordered by frequency it gets more interesting. Tennessee pops straight to the top along with Montana and Kentucky. Through this you can infer that states that are long either the east or west cost ie coastal states do not have many neighboring states as those that are land locked.
The node link diagram is rather boring as it is a simple representation of the Contiguous States. What is more interesting about it is that the states can be reduced to a small graph showing how relatively small the USA is to other countries. Also in this graph you can see the states that are coastal as they are on the outermost edges of the graph. But besides that there is little to be gained from this.
As you mouse over each node the name of the state is displayed for you.
To generate the json file used in for this vis I used the following snippet of python code. What is not included is the shortest path lengths as it proved to produce a very large file wich blocks did not like. So I simply removed that part and ran the script normally.
import json
from collections import defaultdict
import networkx as nx
def stripper(s):
s = s.rstrip("\n")
split = s.split(" ")
return split[0], split[1]
class Node:
def __init__(self, name, numedges):
self.name = name
self.numedges = numedges
def to_jdic(self):
out = {'name': self.name, 'nedges': self.numedges, 'group': 0}
return out
def __str__(self):
return self.name
class Edge:
def __init__(self, source, target):
self.source = source
self.target = target
def to_jdic(self):
out = {'source': self.source, 'target': self.target, 'value': 1}
return out
class shortPlen:
def __init__(self, to, length):
self.to = to
self.length = length
def to_jdic(self):
out = {'to': self.to, 'length': self.length}
return out
def __str__(self):
return "%s of length %d" % (self.to, self.length)
if __name__ == "__main__":
print("hi")
edgeList = defaultdict(list)
nlist = []
nnlist = []
edgel = []
with open('contiguous-usa.dat', "r") as o:
for pairs in map(lambda x: stripper(x), o):
edgeList[pairs[0]].append(pairs[1])
graph = nx.Graph()
graph.edges()
for node in edgeList.keys():
nnlist.append(node)
graph.add_node(node)
for node, edges in edgeList.items():
for edge in edges:
graph.add_edge(node, edge)
if edge not in nnlist:
nnlist.append(edge)
alppathslen = nx.all_pairs_shortest_path_length(graph)
for n in graph.nodes():
spl = alppathslen[n] # type: dict
spl.pop(n)
spl = list(map(lambda x: shortPlen(x[0], x[1]), spl.items()))
nn = Node(n, len(graph.edges(n)))
nlist.append(nn)
nlist = sorted(nlist, key=lambda x: x.name)
nnlist = sorted(nnlist)
for n, e in graph.edges():
edgel.append(Edge(nnlist.index(n), nnlist.index(e)))
g = {}
g['nodes'] = nlist
g['links'] = edgel
with open("contiguous-usa.json", "w+") as out:
out.write(json.dumps(g, default=lambda c: c.to_jdic(), sort_keys=True, indent=1))
Modified http://d3js.org/d3.v3.min.js to a secure url
Updated missing url https://gist.github.com/N0taN3rd/f1ba6bef2fc906b87dbf#file-d3tip-js to https://gist.github.com/n0tan3rd/f1ba6bef2fc906b87dbf#file-d3tip-js
https://d3js.org/d3.v3.min.js
https://gist.github.com/N0taN3rd/f1ba6bef2fc906b87dbf#file-d3tip-js