NetworkX¶
Example¶
In the following example, we create a simple user interface for exploring random graphs with NetworkX.
[1]:
import networkx as nx
First, we create some functions to generate graphs that all have the same signature:
[2]:
import matplotlib.pyplot as plt
def random_lobster(n, m, k, p):
return nx.random_lobster(n, p, p / m)
def powerlaw_cluster(n, m, k, p):
return nx.powerlaw_cluster_graph(n, m, p)
def erdos_renyi(n, m, k, p):
return nx.erdos_renyi_graph(n, p)
def newman_watts_strogatz(n, m, k, p):
return nx.newman_watts_strogatz_graph(n, k, p)
def plot_random_graph(n, m, k, p, generator):
g = generator(n, m, k, p)
nx.draw(g)
plt.show()
Now we create sliders and a drop-down menu for interacting with the plot:
[3]:
from ipywidgets import interact
%matplotlib inline
interact(plot_random_graph, n=(2,30), m=(1,10), k=(1,10), p=(0.0, 1.0, 0.001),
generator={
'lobster': random_lobster,
'power law': powerlaw_cluster,
'Newman-Watts-Strogatz': newman_watts_strogatz,
u'Erdős-Rényi': erdos_renyi,
});
Graphviz¶
Graphviz can be used in two ways:
together with Matplotlib and pygraphviz, see Drawing NetworkX with Matplotlib
together with Graphviz AGraph, see Drawing NetworkX with Graphviz AGraph