More details at: http://networkx.readthedocs.io/en/networkx-1.10/index.html
Source: http://www-personal.umich.edu/~mejn/netdata/ and http://networkdata.ics.uci.edu/data/lesmis/ Code adopted from: https://github.com/networkx/notebooks
Citation: Les Miserables: coappearance network of characters in the novel Les Miserables. Please cite D. E. Knuth, The Stanford GraphBase: A Platform for Combinatorial Computing, Addison-Wesley, Reading, MA (1993).
In [1]:
%matplotlib inline
from operator import itemgetter
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from io import StringIO
import pydotplus
from IPython.display import SVG, display
In [2]:
sns.set_context("poster")
sns.set_style("ticks")
In [3]:
DATA_DIR="../data"
INPUT_NETWORK=os.path.join(DATA_DIR, "lesmis","lesmis.gml")
INPUT_NETWORK
Out[3]:
Please read the following about the GML format for storing networks http://networkx.readthedocs.io/en/networkx-1.10/reference/readwrite.gml.html#format
In [4]:
G = nx.read_gml(INPUT_NETWORK)
#nx.write_gml(G, "../data/lesmis/lesmis.paj.gml")
In [5]:
df_node_degree = pd.DataFrame(list(dict(G.degree()).items()), columns=["node_name", "degree"])
In [6]:
df_node_degree.sort_values("degree", ascending=False).head(10)
Out[6]:
In [7]:
print("radius: {:d}\n".format(nx.radius(G)))
print("diameter: {:d}\n".format(nx.diameter(G)))
print("eccentricity: {}\n".format(nx.eccentricity(G)))
print("center: {}\n".format(nx.center(G)))
print("periphery: {}\n".format(nx.periphery(G)))
print("density: {:f}".format(nx.density(G)))
In [8]:
connected_components = sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)
print("{} connected components found.".format(len(connected_components)))
In [9]:
nx.draw(G)
In [10]:
fig, ax = plt.subplots(1,1, figsize=(16,16))
nx.draw_networkx(
G, with_labels=True,
node_size=[x[1]*10 for x in G.degree_iter()],
pos=nx.spring_layout(G),
node_color="g",
font_size=8,
ax=ax)
ax.axis("off")
Out[10]:
In [11]:
def show_graph(G, file_path):
dotfile = StringIO()
nx.drawing.nx_pydot.write_dot(G, dotfile)
pydotplus.graph_from_dot_data(dotfile.getvalue()).write_svg(file_path)
display(SVG(file_path))
In [12]:
show_graph(G, "../output/lesmis.svg")