1、创建一个无权重的图,并展示
edge_list.csv
a,b,2
a,c,3
b,c,3
d,e,1
d,f,3
e,k,1
r,l,3
t,l,2
import networkx as nx
import matplotlib.pyplot as plt
G =nx.Graph() # 创建无向图
with open('edge_list.csv') as f:
for line in f:
edge = line.strip().split(',')
try:
G.add_edge(edge[0], edge[1])
except:
continue
# 计算连通图
connected_components = nx.connected_components(G)
for component in connected_components:
print(component)
# 绘制图形
colors = ['#008B8B','r','b','orange','y','c','DeepPink','#838B8B','purple','olive','#A0CBE2','#4EEE94']*50
colors = colors[0:len(G.nodes())]
nx.draw_networkx(G,
pos = nx.spring_layout(G),
node_color = colors,
edge_color = colors,
#font_color = colors,