Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
In [1]:
import matplotlib.pyplot as plt
import networkx as nx
Add Node into a Graph¶
In [6]:
graph = nx.Graph()
graph
Out[6]:
In [9]:
graph.add_node(1)
graph.add_node(2)
In [18]:
graph.nodes
Out[18]:
In [19]:
set(graph.nodes)
Out[19]:
In [12]:
1 in graph.nodes(1)
Out[12]:
In [13]:
1 in graph
Out[13]:
In [14]:
graph.nodes[1]
Out[14]:
In [15]:
attr = graph.nodes[1]
In [16]:
attr["x"] = 100
In [17]:
graph.nodes[1]
Out[17]:
In [13]:
graph.nodes[1]
Out[13]:
A node is a hashable object while the attributes of a node is a dict.
In [42]:
type(graph.nodes[1])
Out[42]:
Existing nodes and attributes are reused when adding nodes into a graph.
In [43]:
graph.add_node("Hello", x=10)
In [44]:
graph.nodes
Out[44]:
In [45]:
graph.add_node("Hello", y=20)
In [46]:
graph.nodes["Hello"]
Out[46]:
In [23]:
graph.add_node(("http://github.com/dclong/xinstall", "master"))
In [24]:
graph.nodes
Out[24]:
In [25]:
graph.nodes[("http://github.com/dclong/xinstall", "master")]
Out[25]:
Add Edge into a Graph¶
Adding an edge into a graph automatically add the nodes of the edge into the graph.
In [26]:
graph = nx.Graph()
graph
Out[26]:
In [27]:
graph.add_edge(1, 2)
In [28]:
graph.nodes
Out[28]:
In [29]:
graph.edges
Out[29]:
In [30]:
graph.edges[(1, 2)]
Out[30]:
Existing node is reused when creating an edge.
In [31]:
graph = nx.Graph()
graph
Out[31]:
In [ ]:
In [32]:
graph.add_node(1, x=10)
In [33]:
graph.add_edge(1, 2)
In [1]:
graph.nodes
In [35]:
graph.nodes[1]
Out[35]:
DiGraph¶
In [14]:
graph = nx.DiGraph()
graph
Out[14]:
Get Predecessors/Parents¶
In [15]:
graph.add_edge(1, 2)
graph.add_edge(3, 2)
In [16]:
list(graph.predecessors(2))
Out[16]:
Get Successors/Children¶
In [18]:
list(graph.successors(1))
Out[18]:
Visualization¶
In [16]:
graph = nx.Graph()
In [17]:
graph.add_edge("a", "b", weight=0.6)
graph.add_edge("a", "c", weight=0.2)
graph.add_edge("c", "d", weight=0.1)
graph.add_edge("c", "e", weight=0.7)
graph.add_edge("c", "f", weight=0.9)
graph.add_edge("a", "d", weight=0.3)
In [18]:
elarge = [(u, v) for (u, v, d) in graph.edges(data=True) if d["weight"] > 0.5]
elarge
Out[18]:
In [19]:
esmall = [(u, v) for (u, v, d) in graph.edges(data=True) if d["weight"] <= 0.5]
esmall
Out[19]:
In [27]:
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
nx.draw_networkx(graph, ax=ax)
In [30]:
fig.savefig("/workdir/networkx.png")
In [20]:
pos = nx.spring_layout(graph)
pos
Out[20]:
In [21]:
nx.draw_networkx_nodes(graph, pos, node_size=700)
Out[21]:
In [22]:
nx.draw_networkx_edges(graph, pos, edgelist=elarge, width=6)
Out[22]:
In [23]:
nx.draw_networkx_edges(
graph, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
)
Out[23]:
In [24]:
nx.draw_networkx_labels(graph, pos, font_size=20, font_family="sans-serif")
Out[24]:
In [25]:
plt.axis("off")
plt.show()
In [15]:
import json
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
In [26]:
g = nx.karate_club_graph()
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
nx.draw_networkx(g, ax=ax)
In [ ]: