python有向图,无向图绘制

https://www.jianshu.com/p/52bb142314ebR语言画网络图

https://blog.csdn.net/fly_hawk/article/details/78513257

python绘制无向图,输入数据

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

edges = pd.DataFrame()
edges['sources'] = [0,1,2,3,4,4,6,7,7,9,1,4,4,4,6,7,5,8,9,8]
edges['targets'] = [1,4,4,4,6,7,5,8,9,8,0,1,2,3,4,4,6,7,7,9]
#edges['weights'] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

G = nx.from_pandas_edgelist(edges, source='sources',target='targets')
nx.draw(G, with_labels=True,pos=None, arrows=True)
#plt.savefig("undirected_graph.png")
plt.show()

 

python绘制无向图,读取数据

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
# 读取文件,获取节点和边
f = open("clustered_skill_name.txt", "r")
sources= []
targets = []
while True:
    line = f.readline()
    if line:
            source=line.split('\t')[0]
            target=line.split('\t')[1]
            sources.append(source)
            targets.append(target)
    else:
        break
f.close()
print (len(sources)) #图节点
print(sources)
print(targets)

# 有向图绘制

edges = pd.DataFrame()
edges['sources']=sources
edges['targets'] =targets

G = nx.from_pandas_edgelist(edges, source='sources',target='targets')
nx.draw_networkx(G, pos=None, arrows=True, with_labels=True)
plt.savefig('assitment2009.png')
plt.show()

 

部分txt 文件数据

w

  • 3
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值