python中networkx包学习——画网络图像

来源:互联网 发布:安卓手机长截图软件 编辑:程序博客网 时间:2024/05/21 06:33

简单示例

import networkx as nximport matplotlib.pyplot as pltdef draw_graph(graph):    # extract nodes from graph    nodes = set([n1 for n1, n2 in graph] + [n2 for n1, n2 in graph])    # create networkx graph    G=nx.Graph()    # add nodes    for node in nodes:        G.add_node(node)    # add edges    for edge in graph:        G.add_edge(edge[0], edge[1])    # draw graph    pos = nx.shell_layout(G)    nx.draw(G, pos)    # show graph    plt.show()# draw examplegraph = [(20, 21),(21, 22),(22, 23), (23, 24),(24, 25), (25, 20)]draw_graph(graph)

运行结果

这里写图片描述

————————————————————————————————————————————————————————————————————————

复杂示例

import networkx as nximport matplotlib.pyplot as pltdef draw_graph(graph, labels=None, graph_layout='shell',               node_size=1600, node_color='blue', node_alpha=0.3,               node_text_size=12,               edge_color='blue', edge_alpha=0.3, edge_tickness=1,               edge_text_pos=0.3,               text_font='sans-serif'):    # create networkx graph    G=nx.Graph()    # add edges    for edge in graph:        G.add_edge(edge[0], edge[1])    # these are different layouts for the network you may try    # shell seems to work best    if graph_layout == 'spring':        graph_pos=nx.spring_layout(G)    elif graph_layout == 'spectral':        graph_pos=nx.spectral_layout(G)    elif graph_layout == 'random':        graph_pos=nx.random_layout(G)    else:        graph_pos=nx.shell_layout(G)    # draw graph    nx.draw_networkx_nodes(G,graph_pos,node_size=node_size,                            alpha=node_alpha, node_color=node_color)    nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,                           alpha=edge_alpha,edge_color=edge_color)    nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size,                            font_family=text_font)    if labels is None:        labels = range(len(graph))    edge_labels = dict(zip(graph, labels))    nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels,                                  label_pos=edge_text_pos)    # show graph    plt.show()graph = [(0, 1), (1, 5), (1, 7), (4, 5), (4, 8), (1, 6), (3, 7), (5, 9),         (2, 4), (0, 4), (2, 5), (3, 6), (8, 9)]# you may name your edge labelslabels = map(chr, range(65, 65+len(graph)))#draw_graph(graph, labels)# if edge labels is not specified, numeric labels (0, 1, 2...) will be useddraw_graph(graph)

运行结果:

这里写图片描述

参考

更多详细介绍可参考网站1

原创粉丝点击