This website works better with desktop in both themes, for mobile devices please change to light theme.

Graph Network#

References#

Intro - https://networkx.org/documentation/stable/tutorial.html

Type of graphs - https://networkx.org/documentation/stable/reference/classes/index.html

Network drawing - https://networkx.org/documentation/stable/reference/drawing.html

[1]:
import networkx as nx
import matplotlib.pyplot as plt

Undirected Network#

[2]:
G = nx.Graph()

G.add_edge(1, 1)
G.add_edge(1, 2)
G.add_edge(1, 3)
G.add_edge(1, 4)
G.add_edge(1, 5)
G.add_edge(1, 6)
G.add_edge(6, 7)

fig, ax = plt.subplots(1, 2, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
plt.show()

../_images/GraphNetwork_networkx_exp_4_0.png

Directed Network#

Simple Directed Network#

[3]:
G = nx.DiGraph()

NODES = [1, 2, 3, 4, 5, 6, 7, 8]

G.add_nodes_from(NODES)

G.add_edge(1, 1, weight=10)
G.add_edge(1, 2, weight=5)
G.add_edge(1, 3, weight=3)
G.add_edge(1, 4, weight=4)
G.add_edge(1, 5, weight=8)
G.add_edge(1, 6, weight=15)
G.add_edge(6, 7, weight=7)

fig, ax = plt.subplots(1, 3, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
nx.draw(G, pos=nx.bipartite_layout(G, nodes=NODES),  with_labels=True, font_weight='bold', ax=ax[2])
plt.show()

../_images/GraphNetwork_networkx_exp_7_0.png

Config based Directed Network#

[4]:
G = nx.DiGraph()

NODES = [1, 2, 3, 4, 5, 6, 7, 8]

EDGES = [
    (1, 1),
    (1, 2),
    (1, 3),
    (1, 4),
    (1, 5),
    (1, 6),
    (1, 7),
    (3, 4),
    (3, 6)
]
G.add_nodes_from(NODES)

G.add_edges_from(EDGES)

fig, ax = plt.subplots(1, 3, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
nx.draw(G, pos=nx.bipartite_layout(G, nodes=NODES),  with_labels=True, font_weight='bold', ax=ax[2])
plt.show()
../_images/GraphNetwork_networkx_exp_9_0.png

Complex Directed Network#

[5]:
G = nx.DiGraph()

NODES = [ 'Wake Up', 'Brush', 'Breakfast', 'Go to Work', 'Lunch',
         'Nap', 'Continue To Work', 'Go to Home', 'Dinner', 'TV', 'Sleep']
EDGES = [
    ('Wake Up', 'Brush', {"weight" : 10} ),
    ('Brush', 'Breakfast', {"weight" : 5}),
    ('Breakfast', 'Go to Work', {"weight" : 8}),
    ('Go to Work', 'Lunch', {"weight" : 1}),
    ('Lunch', 'Nap', {"weight" : 12}),
    ('Nap', 'Continue To Work', {"weight" : 8}),
    ('Continue To Work', 'Go to Home', {"weight" : 20}),
    ('Go to Home', 'Dinner', {"weight" : 5}),
    ('Dinner', 'TV', {"weight" : 4}),
    ('TV', 'Sleep', {"weight" : 6}),
#     ('Sleep', 'Wake Up'),
    ('Wake Up', 'Breakfast', {"weight" : 7}),
    ('Wake Up', 'Go to Work', {"weight" : 0.1}),
    ('Go to Work',  'Continue To Work', {"weight" : 0.5}),
    ('Lunch', 'Continue To Work', {"weight" : 0.8}),
    ('Lunch',  'Go to Home', {"weight" : 0.01}),
    ('Go to Home', 'Sleep', {"weight" : 1})
]

G.add_nodes_from(NODES)
G.add_edges_from(EDGES)


options = {
    'node_size': 1000,
    'font_weight' : 'bold',
    'with_labels' : True
}

fig, ax = plt.subplots(1, 2, figsize=(17,8))

nx.draw(G, pos=nx.spiral_layout(G, equidistant=False), ax=ax[0], **options)
nx.draw_networkx_edge_labels(G, pos=nx.spiral_layout(G, equidistant=False), ax=ax[0])

nx.draw(G, pos=nx.circular_layout(G), ax=ax[1], **options)
nx.draw_networkx_edge_labels(G, pos=nx.circular_layout(G), ax=ax[1])

plt.tight_layout()
plt.show()
../_images/GraphNetwork_networkx_exp_11_0.png