Let's start with the snippets. We can load and visualize the network with the following code:
# read the graph (gml format) G = nx.read_gml('lesmiserables.gml',relabel=True) # drawing the full network figure(1) nx.draw_spring(G,node_size=0,edge_color='b',alpha=.2,font_size=10) show()This should be the result:
It's easy to see that the graph is not really helpful. Most of the details of the network are still hidden and it's impossible to understand which are the most important nodes. Let's plot an histogram of the number of connections per node:
# distribution of the degree figure(2) d = nx.degree(G) hist(d.values(),bins=15) show()The result should be as follows:
Looking at this histogram we can see that only few characters have more than ten connections. Then, we decide to visualize only them:
def trim_nodes(G,d): """ returns a copy of G without the nodes with a degree less than d """ Gt = G.copy() dn = nx.degree(Gt) for n in Gt.nodes(): if dn[n] <= d: Gt.remove_node(n) return Gt # drawing the network without # nodes with degree less than 10 Gt = trim_nodes(G,10) figure(3) nx.draw(Gt,node_size=0,node_color='w',edge_color='b',alpha=.2) show()In the graph below we can see the final result of the analysis. This time the graph makes us able to observe which are the most relevant characters and how they are related to each other according to their coappearance through the chapters.