python networkx 网络展示的代码

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,
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值