# 创建一个没有节点和边的空图。
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
nx.draw(G=G,with_labels=True) #
?
?
# G = nx.Graph()
nodes_list = ['a','1'] # 注意 str 和 int
G.add_nodes_from(nodes_list)
nx.draw(G=G,with_labels=True)
?
?
# node_attribute_dict 样式如下:
[
('节点1', {'属性1': '值1', '属性2': '值2'}),
('节点2', {'属性1': '值3', '属性2': '值4'}),
('节点3', {'属性1': '值5', '属性2': '值6'})
]
[('节点1', {'属性1': '值1', '属性2': '值2'}),
('节点2', {'属性1': '值3', '属性2': '值4'}),
('节点3', {'属性1': '值5', '属性2': '值6'})]
node_attributes_dict = [
(4,{"color": "red"}),
(5,{"color": "green"}),
]
G.add_nodes_from(node_attributes_dict)
nx.draw(G=G,with_labels=True)
?
?
# 使用path_graph()创建一个图H
H = nx.path_graph(10)
# 将H中的节点添加到G中
G.add_nodes_from(H)
nx.draw(G=G,with_labels=True)
?
?
G.add_edge(1,5)
nx.draw(G=G,with_labels=True)
?
?
e = (2, 3)
G.add_edge(*e) # 在Python中,*运算符用于解包元组、列表或其他可迭代对象。 等同于G.add_edge(2,3)
nx.draw(G=G,with_labels=True)
?
?
# (u,v)
edges_list = [(1,2),(1,4)]
G.add_edges_from(edges_list)
nx.draw(G=G,with_labels=True)
?
?
G.add_edges_from(H.edges)
nx.draw(G=G,with_labels=True)
?
?
# 定义边属性字典
edge_attributes_dict = {
(1, 2): 1,
(2, 3): 2,
(3, 4): 1.5,
(4, 5): 2.5
}
nx.set_edge_attributes(G=G,values = edge_attributes_dict,name = 'weight')
nx.draw(G=G,with_labels=True)
?
?
G.edges(data = True)
EdgeDataView([(1, 5, {}), (1, 2, {'weight': 1}), (1, 4, {}), (1, 0, {}), (2, 3, {'weight': 2}), (4, 3, {'weight': 1.5}), (4, 5, {'weight': 2.5}), (5, 6, {}), (6, 7, {}), (7, 8, {}), (8, 9, {})])
type(G.nodes())
networkx.classes.reportviews.NodeView
# 查看网络中的节点
list(G.nodes(data = True))
[(1, {}),
(2, {}),
('a', {}),
('1', {}),
(4, {'color': 'red'}),
(5, {'color': 'green'}),
(0, {}),
(3, {}),
(6, {}),
(7, {}),
(8, {}),
(9, {})]
# 指定属性进行查看
list(G.nodes(data = 'color'))
[(1, None),
(2, None),
('a', None),
('1', None),
(4, 'red'),
(5, 'green'),
(0, None),
(3, None),
(6, None),
(7, None),
(8, None),
(9, None)]
# 查看网络中的边
list(G.edges(data = True))
[(1, 5, {}),
(1, 2, {'weight': 1}),
(1, 4, {}),
(1, 0, {}),
(2, 3, {'weight': 2}),
(4, 3, {'weight': 1.5}),
(4, 5, {'weight': 2.5}),
(5, 6, {}),
(6, 7, {}),
(7, 8, {}),
(8, 9, {})]
G.remove_node('a')
# G.remove_nodes_from([1,2,3])
list(G.edges(data = True))
[(1, 5, {}),
(1, 2, {'weight': 1}),
(1, 4, {}),
(1, 0, {}),
(2, 3, {'weight': 2}),
(4, 3, {'weight': 1.5}),
(4, 5, {'weight': 2.5}),
(5, 6, {}),
(6, 7, {}),
(7, 8, {}),
(8, 9, {})]
# 基于节点的color属性为节点着色
G.nodes(data = 'color')
NodeDataView({1: None, 2: None, '1': None, 4: 'red', 5: 'green', 0: None, 3: None, 6: None, 7: None, 8: None, 9: None}, data='color')
# 根据节点属性设置节点颜色
node_colors = [data.get('color', 'blue') for node,data in G.nodes(data = True)]
node_colors
['blue',
'blue',
'blue',
'red',
'green',
'blue',
'blue',
'blue',
'blue',
'blue',
'blue']
nx.draw(G=G,with_labels=True,node_color = node_colors)
?
?
# 1.获取边属性
edge_weights = nx.get_edge_attributes(G, 'weight')
edge_weights
{(1, 2): 1, (2, 3): 2, (4, 3): 1.5, (4, 5): 2.5}
# 2.自定义边的可视化样式,下面是基于weight来设置边的宽度
edge_widths = [edge_weights.get((u, v), 1) for u, v in G.edges()] # 1为默认宽度
edge_widths
[1, 1, 1, 1, 2, 1.5, 2.5, 1, 1, 1, 1]
# 3.可视化
nx.draw(G=G,with_labels=True,node_color = node_colors, width=edge_widths)
?
?
# 也可以设置为权重的倍数
edge_widths = [2*edge_weights.get((u, v), 1) for u, v in G.edges()] # 1为默认宽度
edge_widths
[2, 2, 2, 2, 4, 3.0, 5.0, 2, 2, 2, 2]
# 3.可视化
nx.draw(G=G,with_labels=True,node_color = node_colors, width=edge_widths)
?
?
G_2 = nx.DiGraph()
edges_list = [(1, 2), (1, 3), (1, 4), (3, 4)]
G_2.add_edges_from(edges_list)
nx.draw_networkx(G = G_2)
?
?
# 自定义可视化
edge_labels = nx.get_edge_attributes(G_2, "weight")
pos = nx.spring_layout(G_2)
nx.draw_networkx_edge_labels(G_2, pos, edge_labels=edge_labels) # 绘制图中边的权重
nx.draw_networkx(G_2, pos)
?
?
# 1.gexf
nx.write_gexf(G,'./graph/G_01.gexf')
# 3. 保存为边列表
nx.write_edgelist(G, "./graph/G_01.edgelist")
# 1. read_edgelist
G_read = nx.read_edgelist('./graph/G_01.edgelist')
nx.draw_networkx(G_read)
?
?
# 1. read_gexf
G_read2 = nx.read_gexf('./graph/G_01.gexf')
nx.draw_networkx(G_read2)
?
?