PyNetViz(Python Network Visualization for Sigma.js) as nvs

Author: Kyunghoon Kim ( kyunghoon@unist.ac.kr )
Date created: 10/06/2015
Date last modified: 10/06/2015
Python Version: 2.7

This is a demo for independent gexf, html file. In this case, IPython Notebook Viewer well work.


In [1]:
import networkx as nx
import pynetviz.sigmajs as nvs # pip install pynetviz

Basic Network Draw

Random layout


In [8]:
G = nx.Graph()
G.add_edges_from([(1,2),(2,3),(3,4),(5,6)])
G.add_edge(1, 3)
G.add_edge(1, 6)
nvs.make_gexf(G, filename='./1.gexf')
nvs.make_html(drawEdges='true', gexfname='./1.gexf', filename='./1.html')
nvs.view_html(filename='./1.html')


Spring Layout


In [9]:
G = nx.Graph()
G.add_edges_from([(1,2),(2,3),(3,4),(5,6)])
G.add_edge(1, 3)
G.add_edge(1, 6)
layout = nx.spring_layout(G)
nvs.make_gexf(G, layout, filename='./2.gexf')
nvs.make_html(drawEdges='true', gexfname='./2.gexf', filename='./2.html')
nvs.view_html(filename='./2.html')


Example. Karate club


In [10]:
G = nx.karate_club_graph()

Network Information


In [11]:
print "Number of Nodes : ", nx.number_of_nodes(G)
print "Number of Edges : ", nx.number_of_edges(G)
degreelist = list(G.degree().values())
print "Avg. Node Degree : ", float(sum(degreelist))/nx.number_of_nodes(G)
try:
    print "Avg. Path Length : ", nx.average_shortest_path_length(G)
except nx.NetworkXError as e:
    print e
print "Avg. Clustering Coefficient : ", nx.average_clustering(G)


Number of Nodes :  34
Number of Edges :  78
Avg. Node Degree :  4.58823529412
Avg. Path Length :  2.40819964349
Avg. Clustering Coefficient :  0.570638478208

Change a height of iframe


In [12]:
layout = nx.spring_layout(G)
nvs.make_gexf(G, layout, filename='./3.gexf')
nvs.make_html(drawEdges='true', gexfname='./3.gexf', filename='./3.html')
nvs.view_html(height=1000, filename='./3.html')


Change a layout using spectral_layout


In [13]:
layout = nx.spectral_layout(G)
nvs.make_gexf(G, layout, filename='./4.gexf')
nvs.make_html(drawEdges='true', gexfname='./4.gexf', filename='./4.html')
nvs.view_html(height=1000, filename='./4.html')


Change a height of iframe


In [14]:
nvs.make_gexf(G, layout, filename='./5.gexf')
nvs.make_html(drawEdges='true', gexfname='./5.gexf', filename='./5.html')
nvs.view_html(height=300, filename='./5.html')


Disable for edge


In [15]:
nvs.make_gexf(G, layout, filename='./6.gexf')
nvs.make_html(drawEdges='false', gexfname='./6.gexf', filename='./6.html')
nvs.view_html(height=300, filename='./6.html')


Degree Centrality


In [16]:
nx.degree_centrality(G)


Out[16]:
{0: 0.48484848484848486,
 1: 0.2727272727272727,
 2: 0.30303030303030304,
 3: 0.18181818181818182,
 4: 0.09090909090909091,
 5: 0.12121212121212122,
 6: 0.12121212121212122,
 7: 0.12121212121212122,
 8: 0.15151515151515152,
 9: 0.06060606060606061,
 10: 0.09090909090909091,
 11: 0.030303030303030304,
 12: 0.06060606060606061,
 13: 0.15151515151515152,
 14: 0.06060606060606061,
 15: 0.06060606060606061,
 16: 0.06060606060606061,
 17: 0.06060606060606061,
 18: 0.06060606060606061,
 19: 0.09090909090909091,
 20: 0.06060606060606061,
 21: 0.06060606060606061,
 22: 0.06060606060606061,
 23: 0.15151515151515152,
 24: 0.09090909090909091,
 25: 0.09090909090909091,
 26: 0.06060606060606061,
 27: 0.12121212121212122,
 28: 0.09090909090909091,
 29: 0.12121212121212122,
 30: 0.12121212121212122,
 31: 0.18181818181818182,
 32: 0.36363636363636365,
 33: 0.5151515151515151}

Change of a node size with degree centrality


In [17]:
layout = nx.spring_layout(G)
nvs.make_gexf(G, layout, size=nx.degree_centrality(G), filename='./7.gexf')
nvs.make_html(drawEdges='true', gexfname='./7.gexf', filename='./7.html')
nvs.view_html(height=500, filename='./7.html')


Change of a node size with betweenness centrality


In [18]:
nvs.make_gexf(G, layout, size=nx.betweenness_centrality(G), filename='./8.gexf')
nvs.make_html(drawEdges='true', gexfname='./8.gexf', filename='./8.html')
nvs.view_html(height=500, filename='./8.html')


Change of a node size with closeness centrality


In [19]:
nvs.make_gexf(G, layout, size=nx.closeness_centrality(G), filename='./9.gexf')
nvs.make_html(drawEdges='true', gexfname='./9.gexf', filename='./9.html')
nvs.view_html(height=500, filename='./9.html')


Change of a node size with clustering coefficient


In [20]:
nvs.make_gexf(G, layout, size=nx.clustering(G), filename='./10.gexf')
nvs.make_html(drawEdges='true', gexfname='./10.gexf', filename='./10.html')
nvs.view_html(height=500, filename='./10.html')


Change of a node size * 10 with degree centrality


In [21]:
nvs.make_gexf(G, layout, size=[c*10 for c in nx.degree_centrality(G)], filename='./11.gexf')
nvs.make_html(drawEdges='true', gexfname='./11.gexf', filename='./11.html')
nvs.view_html(height=500, filename='./11.html')



In [ ]: