networkx的使用

需要装的库:numpy,matplotlib

代码:

import networkx as nx
import matplotlib.pyplot as plt
import random

def get_vertice_and_edge(graph_path):
    f_r=open(graph_path,'r')
    all_lines=f_r.readlines()
    grph=[]
    num=0
    for line in all_lines:
        line=line.split(' ')
        if line[0]=='e':
            if random.randint(0,1)==1 and num<=900:
                ed=(line[2],line[3],int(line[3]))
                grph.append(ed)
                num+=1
    return grph

if __name__=="__main__":
    G=nx.Graph()
    g=get_vertice_and_edge("2015-9-7-11zhong.txt")
    G.add_weighted_edges_from(g)
    nx.draw(G,font_size=10,node_size=30,with_labels = False)
    plt.show()

猜你喜欢

转载自blog.csdn.net/Yansixiliang/article/details/79487964