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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐